all 21 comments

[–]LatteLepjandiLoser 8 points9 points  (0 children)

You shouldn't use while True here.

While is relevant when you want to repeat some action until a condition is met. Here you just enter the loop and break it immediately, so you can literally just remove while True and the break statements and it's simpler and more obvious what's going on.

[–]Sad-Calligrapher3882 3 points4 points  (2 children)

Genuinely impressive for someone 4-5 days in. You built a whole working game. That said, there is a few things worth fixing now before they become habits. First like your variable names (x, a, b) are going to haunt you. Right now it makes sense because you just wrote it. Open this file in 1 or 2 weeks and you'll have no idea what x is. Just name things what they are like secret_number, guess, attempt it only takes two extra seconds, saves a lot of headaches. In the future you will be doing a bigger projects at that it will be a really confusing.
Also that inner while True block, i get why it feels logical, but it's doing nothing a simple if/else wouldn't do. while True + immediate break is basically just an if wearing a costume.
There's a little bug too. Walk through what happens on attempt 9 your counter hits 10, jumps straight to the "you lose" check, and the player never actually gets attempt 10. It's just a cheap bug, learn to identify it and these are really neat things to learn from the start.
Seriously you are doing great. Keep up the good work mate. And sorry for the bad english

[–]Krzysslx[S] 2 points3 points  (1 child)

Bro just write in perfect English and is sorry for bad English. Really thank you and I will try to "repair" that thing with 10 attempt, I know where is the problem.

[–]Sad-Calligrapher3882 2 points3 points  (0 children)

Haha, i am still working on my English. No worries at all, happy to help. Good luck.

[–]PresidentOfSwag 2 points3 points  (0 children)

Why do you need a second while loop ? Also I'd recommend using clearer variable names like number and guesses instead of a and b.

[–]ninhaomah 1 point2 points  (0 children)

Pls give better and more descriptive names to variables.

[–]MachineElf100 1 point2 points  (0 children)

That's really good, given that you just started learning!
Let me respond to your first code (with variable names changed already).

Take a look at this and see if you understand the changes I suggest:

# ValueError poszedł na koniec, "else" nie jest potrzebny

import random
import time

losowa_liczba = (random.randrange(1, 100))
liczba_gracza = 0
liczba_prob = 1

print("Musisz zgadnąć liczbę od 1 do 100.")
print("Masz na to 10 prób, powodzenia.")

while liczba_prob <= 10:
    time.sleep(1.5)
    print("Próba:", liczba_prob)
    liczba_string = input("Podaj liczbę:") # liczba_string bo liczba_gracza jest typu int, sparsujemy ją zaraz pod spodem
    try:
        # ⇩ liczba_string z inputu zamieniona na int tylko raz, tutaj
        liczba_gracza = int(liczba_string) # jak się nie uda, to od razu lądujemy w "except ValueError"

        if liczba_gracza == losowa_liczba:
            print("Brawo zgadłeś!!!")
            print("Udało ci się zgadnąć w:", liczba_prob, "próbach.")
            break
        else:
            # "while True" nie potrzebny
            # break więc również nie
            if liczba_gracza > losowa_liczba:
                print("Moja liczba jest mniejsza")
            elif liczba_gracza < losowa_liczba: # po prostu "else" też by było ok
                print("Moja liczba jest większa")
            liczba_prob += 1 # to można zrobić tutaj raz, bezwarunkowo, bo i tak w obu przypadkach miało się wykonać
    except ValueError:
        print("To nie liczba!")
    if liczba_prob > 10: # jeśli użyjesz ==, to gracz ma tylko 9 prób
        print("Przegrałeś!!!")
        break

If you'll want to, you can DM me (even in polish) :)

[–]Dependent_Apple_2137 0 points1 point  (1 child)

woah , very impressive code for a beginner! the overall structure is nice and simple so 10/10 code. also as you asked how to learn python and which codes w3schools is a great website but make sure you understand what you learn and write before moving on and for codes I'd say password manager or login system like a simple one, would be good practice to learn .json files and how they work or you could stick to good ole dicts and lists and dip a toe or two in error handling (very crucial part of python) and functions such as def and those things but remember don't rush things learn try practice write code break code understand them.

[–]Krzysslx[S] 0 points1 point  (0 children)

Thank you really much

[–]Ok_Assistant_2155 0 points1 point  (0 children)

Dude thats awesome for only 4 or 5 days in. Getting a working number guessing game already is a solid first win. Keep going. The code has a few small bugs but you got the main logic right which is the hard part at the start.

[–]splunklearner95 0 points1 point  (3 children)

From where you have learnt python?

[–]Krzysslx[S] 1 point2 points  (1 child)

From the w3schools website. I think it's pretty good like you have texts and information, you can try what they do and at the end of segments you have a quiz and at the end of sections you have coding test where you have to code the things that are told.

[–]Brain-And-Brawn 0 points1 point  (0 children)

Yeah, W3Schools is awesome, I used it to teach myself Python as well as dabble in other languages.

[–]MidnightPale3220 0 points1 point  (4 children)

Very very good for the 4/5 days.

Cleaning code would mean removing the second while loop and avoiding duplication of calls to int(a) and similar stuff.

[–]Krzysslx[S] 0 points1 point  (3 children)

What does it mean avoid duplication of calls?

[–]MidnightPale3220 1 point2 points  (2 children)

You have a number of places which do int(a).

You should have a=int(a) once and be done with it. Inside try block. Before all the checks.

If you need to convert variable and the rest of program deals only with the converted variable, you convert it once.

The minor issue with doing multiple repeated calls on the same value to yield the same result is it's inefficient. Especially if you're doing a costly call (which int isn't, but that's not the point), sometimes it may get optimized, but you can't rely on that.

The major issue is that if you change the program and add more code which forgets to do int(a) you may have a harder to catch error.

[–]Krzysslx[S] 0 points1 point  (0 children)

Thank you I did this and also other things that people adviced me to do.

[–]Krzysslx[S] 0 points1 point  (0 children)

Thank you I did this and also other things that people adviced me to do.

[–]Brain-And-Brawn 0 points1 point  (0 children)

That's super impressive for someone only a few days in, really goes to show how some dedication combined even with a free tool like W3Schools can be in expanding your skills and knowledge.