python - can i use while loop to limit the user variable -


this question has answer here:

import random >>> pc = random.randint(0,5) >>>  >>> while user > 0 & user < 6:     while true:      user = int(input("enter no"))     if user < pc :         print("low")     elif user > pc:         print("high")     else :         print("correct")         break 

here trying limit user 0--6. if enters beyond range can not proceed

you can use while loop read user input, validate user's input, if meets needs, break loop, otherwise, can continue loop ask input.

for python3:

while true:     try:         user = int(input('choose number: '))     except valueerror:         print("not number")         exit(-1)     if 0 <= user <= 6:         break     else:         print("make sure input between 0 6")  # process user variable print(user) 

Comments

Popular posts from this blog

node.js - Node js - Trying to send POST request, but it is not loading javascript content -

javascript - Replicate keyboard event with html button -

javascript - Web audio api 5.1 surround example not working in firefox -