0

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.")
Pallu08
  • 25
  • 6
  • 1
    Possible duplicate of [Asking the user for input until they give a valid response](https://stackoverflow.com/questions/23294658/asking-the-user-for-input-until-they-give-a-valid-response) – rdas May 29 '19 at 03:44

2 Answers2

0

Ok, so I'm not sure how your system of if statements and while loops works, but I do know that you are using the input function incorrectly.

If you want to check that an input is equal to a thing, you should do: if input('>>> ') == 'a thing'

That will output >>>, the user can input their response, and it will return true if it is equal to a thing.

I think I know what this does, but I am not sure. I will include what I would do to do this at the end of this.

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()
while True:
    if x<y:
        print("Too low. Try again.")
        int(input())
    if x>y:
        print("Too high. Try again.")
        int(input())

I think you are trying to check if the user can guess it in one go, and then tell them if their guess is too high or too low on any following guesses. I would do it like this:

import random

print('guess a number game (1-100 range)')

y = random.randint(1, 101)

x = int(input('>>> '))

if x == y:
    print('yay, you got it first try')
    exit()
else:
    print('nope')
    while x != y:
        x = int(input('>>> '))
        if x < y:
            print('Too low!')
        elif x > y:
            print('Too high!')
        elif x == y:
            print('E P I C')
            exit()

Here is an example output:

guess a number game (1-100 range)
>>> 48
nope
>>> 50
Too high!
>>> 25
Too low!
>>> 35
Too low!
>>> 45
Too high!
>>> 40
Too low!
>>> 44
E P I C

I have not been on here in a while (haha), so this might not be the best answer. Hope this helps, though!

Jack H.
  • 115
  • 10
0

I think this is the pattern what you looking for.

import random

EXIT = False

while True:
  if EXIT:
     break
  y=random.randint(1,101)
  print("Random ",y)
  while not EXIT:
    print("Guess an integer between 1-100.")
    x=int(input())
    if x==y:
        print(x, "is correct! Would you like to try again? (Y/N)\n")
        z = input()
        if z == 'Y':
            print("Too bad! This game isn't finished yet!\n")
            break
        if z == 'N':
            print("Good! This game isn't finished yet!\n")
            EXIT = True
            break
    else:
        if x<y:
            print("Too low. Try again.\n")
        if x>y:
            print("Too high. Try again.\n")

The output will be .

Random  89
Guess an integer between 1-100.
20
Too low. Try again.

Guess an integer between 1-100.
100
Too high. Try again.

Guess an integer between 1-100.
89
89 is correct! Would you like to try again? (Y/N)

Y
Too bad! This game isn't finished yet!

Random  36
Guess an integer between 1-100.
36
36 is correct! Would you like to try again? (Y/N)

N
Good! This game isn't finished yet!
Ajmal JK
  • 813
  • 4
  • 14