简体   繁体   中英

Python - Problem coding random number guessing game using while loop

first time poster. I'm having a problem getting my random number guessing game in Python to work properly. I've purposely set the number to not be random (it's 50) for testing purposes. If I guess a number under 50, it will tell me that the number is higher; I can then guess higher than 50 and it will tell me lower.

For some reason though, if I guess above 50 and then try to guess under 50, the program stops.

Can someone point me in the right direction please? Here is my code:

from random import randint
randomNum = 50 #randint(0,100)
print("Let's play a guessing game.  I'm thinking of a number between 0-100. Take a guess 
what it is") 
num = int(input()) 
if(num == randomNum):
    print("Wow, what fantastic luck!  You're right!")
while (randomNum > num):
    print('Its higher than that. Guess again!')
    num = int(input())
    while (num == randomNum):
        print("You got it!")
        break

while (randomNum < num):
    print('Its lower than that! Guess again!')
    num = int(input())
    while (num == randomNum):
        print("You got it!")
        break

I see the issue, when you guess first, you enter your first while loop

while (randomNum > num):
    print('Its higher than that. Guess again!')
    num = int(input())
    while (num == randomNum):
        print("You got it!")
        break

then you guess again with that second input, but if your number isnt higher than 50, then that while loop is over, and the program ends, wrapping th whole thing in a while loop and putting your input at the top like this:

num ='anything thats not an int'
while (num != randomnum):
    num = int(input())
    
    if randomNum > num:
        print('Its higher than that. Guess again!')

    elif randomNum < num:
        print('Its lower than that! Guess again!')
print('you guessed the number!')

the last print statement is outside the loop cause the loop will only break once num = randomnum

im coming from CodeGolf so excuse my godawful readability, ill try my best to explain it.

while 1:
 try:n=hash("1")%int(input("Level: "))+1;break
 except:1
i=""
while i!=n:
 try:i=int(input("Guess: "))
 except:continue
 print("Too large!"if i>n else"Too small!"if i<n else"Just right!")

this part here:

while 1:
 try:n=hash("1")%int(input("Level: "))+1;break
 except:1

is a while loop that detects a valid number (which is the highest number n can be), and creates a random number from it:

while True: try to turn the input into an int then break out the loop, and if it fails, try ask for input again

ill explain this part next:

i=""
while i!=n:
 try:i=int(input("Guess: "))
 except:continue
 print("Too large!"if i>n else"Too small!"if i<n else"Just right!")

make a variable called i (input) and set it to nothing, create a while loop with the condition being i not equaling n (the random number between the user specified range) next, try get input. the try/except loop is to ensure a number is being supplied (and not a string. if a string was supplied it would give an error, and retry the input prompt until a number is given) finally, is the print, if the input is lower than n, it prints loo low and vise versa, and if the input is equal to the random number, it will print just right, and the while loop will be fulfilled, ending the program!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM