I'm trying to create a simple number guessing game. It's to my understanding that I can use a while loop as a proper goto
function if the user input does not equal the number generated by the RNG. Included is the code I've created. What I'm trying to do is tell the program If x<y
, print("Too low. Try again")
, then loop back to ask the user for input. Same principle if x>y
. When the number is guessed correctly, it's supposed to display a message, ask for a Y/N input, then close the program or loop back to the beginning. How would I go about doing this?
I've tried including a while loop, but I'm faced with the following problem: Let's say the RNG generates 50. If I guess 49, it prints the x<y
message, asks for another input, but it will keep displayed the x<y
message even if in the new input x>y
or x==y
.
Also, when the answer is guessed correctly, I'm not sure how to make it so the program restarts if they answer Y.
Lastly, when the game prompts users to answer Y/N, the terminal will display the letter "Y" for some reason before the user has a chance to input.
print()
print("Guess an integer between 1-100.")
import random
y=random.randint(1,101)
print(y)
x=int(input())
if x==y:
print(x, "is correct! Would you like to try again? Y/N")
if input("Y"):
print("Too bad! This game isn't finished yet!")
exit()
if input("N"):
print("Good! This game isn't finished yet!")
exit()
else:
if x<y:
print("Too low. Try again.")
if x>y:
print("Too high. Try again.")