简体   繁体   中英

Infinite loop after input from the user

#python game import random a = random.randint(1,100) b = int (input("guess the number")) while True : if a == b: print ("you guessed it correct \n you won the game!!") break elif a>= b: print ("you guesssed it too high") else : print ('you guessed it too low')

You have to get the input in the loop rather than before the loop:

a = random.randint(1,100)

# b = int (input("guess the number")) <--- DELETE

while True :

    b = int (input("guess the number")) # <--- INSERT

    if a == b:
        print ("you guessed it correct \n you won the game!!")
        break        
    elif a >= b:
        print ("you guesssed it too high")        
    else:
        print ('you guessed it too low')

Put the input in the loop.

import random

a = random.randint(1,100)

while True :
    b = int (input("guess the number"))
    if a == b:
        print ("you guessed it correct \n you won the game!!")
        break

    elif a>= b:
        print ("you guesssed it too high")

    else :
        print ('you guessed it too low')

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