New to Python! Need Help with a simple Guess the number game. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

New to Python! Need Help with a simple Guess the number game.

Forgive the dumb question. I'm new to coding, on my 4th day. I have followed a video to program a simple Guess the number game. The video ends with the code ending when the user either guesses the number or failings to guess the number. I would like to get the user's input as to try again or to quit. If they want to try again to get their input and re start the code, if they choose not to try again to end the code. This is what I have so far. I would like to know what I need to do and why. Thank you. while True: print(" Can you guess the number that I'm thinking of? It's between 1 and 10.") secret_number = 3 guess_count = 0 guess_limit = 3 while guess_count < guess_limit: guess = int(input('Guess: ')) guess_count += 1 if guess == secret_number: print('You guessed right!') else: print(" You didn't guess the number I was thinking of.) print(" would you like to try again?") while True: answer = raw_input('Run again? (y/n): ') if answer in ('y', 'n'): break print 'Invalid input.' if answer == 'y': continue else: print 'Goodbye' break

30th Aug 2019, 12:54 AM
Jason Lively
Jason Lively - avatar
2 Answers
+ 4
What I would recommend doing is taking a look back at the tutorial as to help you get used to Python's syntax. It can be difficult to work with at times, but once you get the hang of it it becomes relatively easy haha. For that section, here's the logic I would follow: As there wouldn't really be a need for a second while loop, just get rid of that loop and place all the code within the first one, following everything that came before. Take in user input, and similar to what you did, check if it's either 'y' or 'n'. If you find that it isn't then print something and exit the loop, but if it is then see which of the 2 values it is and either restart the loop or break out of it, depending on what the value was. Hope this helped!
30th Aug 2019, 4:06 AM
Faisal
Faisal - avatar
+ 3
From the looks of it, you seem to have a couple problems. Firstly, indentation seems to be off in some areas. It seems that for your code to work, everything needs to be placed within the while True loop, so make sure that your indents ensure that each line of code following that are actually in the loop. While not too major, you seem to be using raw_input() to get input from the user, and while it isn't the worst thing to use, it isn't supported in Python3 as in the new version, input() performs the exact same as raw_input() so it was deprecated. If that's raising errors for you, you can just replace it with input() as it does the same thing. Following line 15, your code seems to fall apart a little. Your conditional seems to break the loop if the answer is either y or n, print statements seem to be wrong (you have to include the brackets and place whatever you want to be printed in quotation marks, like print("hello")), and indentation is off as well (continued)
30th Aug 2019, 4:02 AM
Faisal
Faisal - avatar