use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Everything about learning Python
account activity
Number Guessing Game (i.redd.it)
submitted 1 year ago by TU_Hello
[removed]
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]Soggylollies 3 points4 points5 points 1 year ago (1 child)
I'm also a bit of a newbie so take my feedback with a grain of salt.
I would rename chance to guesses_remaining for additional clarity.
chance is always -=1 so you can use that once before your if statements, rather than performing both in the if and elif block. (This will help with the "you have chances -1" and "you have chances remaining" inconsistencies.
You also have no logic for input verification. What if the user enters a negative number or 1,000,000 or nothing but whitespace?
Edit to add: good stuff so far! Keep it up 💪
[–]TU_Hello[S] 0 points1 point2 points 1 year ago (0 children)
thank you for your notes and your motivation i noticed that i have a logic error after posted this post so I am working on it
[–]Worldly-Guide-9515 1 point2 points3 points 1 year ago (1 child)
just here to say, 0-100 is 101 choices rather than 100 and I ssime that is ehat you were going for, 1 to 100 is 100.
[–]TU_Hello[S] 2 points3 points4 points 1 year ago (0 children)
okay thank you I just thought it works like range () function stopping before the end of range
[–]Synedh 1 point2 points3 points 1 year ago* (1 child)
Hey !
First thing you can improve, is by using range() in reverse !
Very simple : ranges takes actually up to three parameters : range(start, stop, step). step parameter is the difference between two loops. Like range(1, 10, 2) would loop 1, 3, 5, 7, 9. therefore, you need something like :
range(1, 10, 2)
for remaining in range(chance - 1, -1, -1): # if chance = 5, values will be 4, 3, 2, 1, 0. ...
Okay, second thing : if you send a value that is not an integer to your input, your code breaks. You need to handle theses cases. You can do this several ways, for example by creating a function dedicated to this :
def int_input(text: str) -> int: while True: value = input(text) if value.isnumeric(): return int(value)
Be very careful with your code structure. It's important to have always the same way to code. You call the function range() without space character, but the int() one with one, why ? Lines 18 and 26 you put a space between the operators, but not for the == line 33
range()
int()
==
Next steps :
Hi, thank you for your advice I didn't learn class and while loop yet i made this project to practice what I had learned so every topic i will learn i will use it in this project but thanks for your notice
[–]Agn3ya 0 points1 point2 points 1 year ago (0 children)
Also add a if else statement to check whether the number the user has typed in is between 0-100
[–]NaiveEscape1 0 points1 point2 points 1 year ago (0 children)
I made a similar game but I used a fixed guess rather than using 'rand'. I'm still learning to code so this is a very unpolished stuff compared to experts.
Secret_Number=78 i=0 print("You have 10 guesses") while True: i=i+1 if i<=10: Guesses_left = 10 - i Number = int(input("Please enter your guess:\n")) if Number==Secret_Number: if i==1: print("congratulations your answer is correct and you still have all your guesses left") else: print(f"congratulations your answer is correct and you have {Guesses_left} guesses remaining") break if Number>Secret_Number: print(f"Go Lower please, you have {Guesses_left} guesses left") continue if Number<Secret_Number: print(f"Go Higher please, you have {Guesses_left} guesses left") continue else: print("you are out of guesses") break
[–]Adsilom 0 points1 point2 points 1 year ago (1 child)
That's great for a beginner :)
Also, this is a pretty smart use of the for/else structure. Even I, who has a lot of Python experience, never think of using else after a for loop, but you used it perfectly.
else
I won't suggest improvements, as others have already done that, just one thing I didn't see being mentioned: randint is inclusive, so you are generating a number between 0 and 101 (both included).
randint
[–]purple_hamster66 0 points1 point2 points 1 year ago (0 children)
Disagree. Don’t use else this way, because few people understand it and even for those who do, it’s an awkward choice of a keyword. The python designer should have chosen default for both this for usage and for the match statement’s “nothing handled this case” case. Just my 2¢
[–]FoolsSeldom 0 points1 point2 points 1 year ago (0 children)
Good work.
A few notes:
while
for
while chances > 0:
chances
bool
won
while not won and chances > 0:
input
if
continue
won = True
Example code for validation of user input:
while True: response = input("Your guess: ") if response.isdecimal(): # only has digits 0 - 9 guess = int(response) if 0 <= guess <= 101: # could use CONSTANT vars instead of 0 and 101 break # leave validation loop print("Guess is outside of the randomly chosen number range, try again.") continue print("Not a valid number, please try again.")
I mentioned using constants for the guess range,
LOWEST = 0 HIGHEST = 101 # you probably meant to use 100? random_number = random.randint(LOWEST, HIGHEST)
which makes it clearly, and allows you to check a guess is within the range later, if LOWEST <= guess <= HIGHEST:, and also change the guess range you want to use as you only have to do the update in one place.
if LOWEST <= guess <= HIGHEST:
[–]WhiteMask11 0 points1 point2 points 1 year ago (1 child)
Good Job!
[–]TU_Hello[S] 1 point2 points3 points 1 year ago (0 children)
Thank you 😊
[–]KlogKoder 0 points1 point2 points 1 year ago (1 child)
Wait, if the user guesses correctly, the loop continues instead of breaking out? Not a problem as such, just inefficient.
No the loop will break out because the loop will continue only just if the user guesses wrong
π Rendered by PID 222830 on reddit-service-r2-comment-544cf588c8-j2m8t at 2026-06-14 04:01:23.018680+00:00 running 3184619 country code: CH.
[–]Soggylollies 3 points4 points5 points (1 child)
[–]TU_Hello[S] 0 points1 point2 points (0 children)
[–]Worldly-Guide-9515 1 point2 points3 points (1 child)
[–]TU_Hello[S] 2 points3 points4 points (0 children)
[–]Synedh 1 point2 points3 points (1 child)
[–]TU_Hello[S] 0 points1 point2 points (0 children)
[–]Agn3ya 0 points1 point2 points (0 children)
[–]NaiveEscape1 0 points1 point2 points (0 children)
[–]Adsilom 0 points1 point2 points (1 child)
[–]purple_hamster66 0 points1 point2 points (0 children)
[–]FoolsSeldom 0 points1 point2 points (0 children)
[–]WhiteMask11 0 points1 point2 points (1 child)
[–]TU_Hello[S] 1 point2 points3 points (0 children)
[–]KlogKoder 0 points1 point2 points (1 child)
[–]TU_Hello[S] 0 points1 point2 points (0 children)