Hello everyone! I haven't been able to spend time programming for some months now, so I decided to brush up my very few skills by writing simple programs. I opted for "Guess the number" since it's pretty basic stuff (or so i thought). My main trouble was that I wanted to let the player quit by typing "quit", instead of asking for a number, for example "0".
If any other newbie has tried out this program, the problem is when the user tries to input anything other than a number. Python (not without reason) doesn't type-cast it into anything with int(), so the program crashes. The following is my solution to this problem, but it doesn't look very elegant at all.
I would appreciate any suggestions on making the code better. Thanks!
from random import * print("\n* * * Welcome! * * *\n\nGo ahead and try to guess a number from 1 to 10. If you feel like quitting, type QUIT\n") seed() playing = True validating = True number = randint(1,10) while playing: while validating: guess = input("Your guess? ") try: int(guess) break except: if str.upper(guess) == "QUIT": print ("Goodbye!") validating = False playing = False else: print("\nI don't think that was a number\n") if playing == False: break if int(guess) == 0: print ("Goodbye!") playing = False elif int(guess) < number: print("\nA little higher...\n") elif int(guess) > number: print("\nA little lower...\n") else: print("\nYou got it!\n") playing = False