mercredi 3 août 2022

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



Aucun commentaire:

Enregistrer un commentaire