Jun-03-2020, 09:35 AM
Hi everybody!
I'm trying to learn python from a book written by Eric Matthes. Right in the introduction he mentions how, as a child, he programmed a number guessing game, which got me thinking - if a kid can do that, surely I can as well!
It turns out that I can, though not without some bumps. The code I've written is as follows:
1. The number of tries the program show is sometimes right and other times wrong. I'm certain it has something to do with where I place the count variable, but having read the code several times, I'm unable to figure out where the problem is.
2. How would I get the program to go back to the start of the main if loop (started on line 6) if the user triggers the else at line 67 by answering anything other than yes or no? I imagine adding a while loop with the play variable being different to yes or no, but I'm not sure exactly how or where I'd go about doing that.
3. Not one of the main two issues, but really, any sort of comment or advice, such as how to make it shorter, neater or more efficient, would be appreciated.
Thanks in advance, and sorry if the questions might have been answered elsewhere not requiring a new thread!
Chris
I'm trying to learn python from a book written by Eric Matthes. Right in the introduction he mentions how, as a child, he programmed a number guessing game, which got me thinking - if a kid can do that, surely I can as well!
It turns out that I can, though not without some bumps. The code I've written is as follows:
import random number = random.randint(0,100); user = input ("Greetings, new user. To continue, please enter your name: "); print (f"Hello, {user.title()}. \nI am Guessatron 1.0, Master of Mystery and Enabler of Entertainment.\nI can provide you with endless hours of fun"); play = input ("Would you like to play a game? "); if play.lower() == "no": print (f"Ah, well, that's unfortunate. You probably assume you can find this high quality of entertainment elsewhere. \nYou assume wrong. \nOh well, no second chances. Good bye, {user.title()}!"); exit(); elif play.lower() == "yes": game_on = True print ("Excellent choice, let's begin!"); guess = input ("I'm thinking of a number between 0 and 100. Try to guess it: "); guess = int(guess); count = 1; win_count = 0; if guess == number: print ("Wow, nailed it on your first guess, you must be psychic! Congratulations!"); win_count += 1 game_on = False replay = input ("Would you like to try again? "); if replay.lower() == "yes": guess = input ("All right, let's go one more time! What's your next guess? "); guess = int(guess); count +=1; game_on = True elif replay.lower() == "no": print (f"Well, you've already impressed me with your guessing prowess once, {user.title()}! You should always trust your insticts!") exit(); while game_on == True: while guess > number: guess = input ("Not quite, the number I'm thinking is lower. Try again: "); guess = int(guess); count += 1; if guess == number: print (f"Congratulations, you got it, {number} is right! It took {count} tries."); count = 0; win_count += 1; game_on = False replay = input ("Would you like to try again? "); if replay.lower() == "yes": number = random.randint(0,100); guess = input ("All right, let's go one more time! What's your next guess? "); count += 1; guess = int(guess); game_on = True; elif replay.lower() == "no": print (f"Well, you've already impressed me with your guessing prowess {win_count} times, {user.title()}! You should always trust your insticts!") exit(); while guess < number: guess = input ("No, the number I'm thinking is higher, try again: "); guess = int(guess); count += 1; if guess == number: print (f"Congratulations, you got it, {number} is right! It took {count} tries."); count = 0; win_count +=1; game_on = False; replay = input ("Would you like to try again? "); if replay.lower() == "yes": number = random.randint(0,100); guess = input ("All right, let's go one more time! What's your next guess? "); guess = int(guess); game_on = True; elif replay.lower() == "no": print (f"Well, you've already impressed me with your guessing prowess {count} times, {user.title()}! You should always trust your insticts!") exit(); else: play = input (f"I don't really understand what you just said, {user.title()}. It was a pretty straightforward question, please provide a straightforward answer: ")I have two issues I've been unable to solve:
1. The number of tries the program show is sometimes right and other times wrong. I'm certain it has something to do with where I place the count variable, but having read the code several times, I'm unable to figure out where the problem is.
2. How would I get the program to go back to the start of the main if loop (started on line 6) if the user triggers the else at line 67 by answering anything other than yes or no? I imagine adding a while loop with the play variable being different to yes or no, but I'm not sure exactly how or where I'd go about doing that.
3. Not one of the main two issues, but really, any sort of comment or advice, such as how to make it shorter, neater or more efficient, would be appreciated.
Thanks in advance, and sorry if the questions might have been answered elsewhere not requiring a new thread!
Chris