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
Post a Comment