简体   繁体   中英

Comparison operator in While Loop with Tuple() or List[] Python 3

tup=(100,200,300,400,500)
userinput=0
while userinput != tup:
 userinput=int(input("Try again"))

x=userinput

So here is a small problem i want to take input from user until user is not entering one of the same value as in tup variable. and once user added one of them same value as is in the tup variable then i want to add/move this value to x variable.

Use the "in" operator.

tup=(100, 200, 300, 400, 500)
usrInput = 0
while usrInput not in tup:
    usrInput = int(input("Please enter a number: "))

The program will keep asking the user for a number until they enter one which is in the tuple.

用户的int不能等于整个元组,因此您应该考虑如何根据元组中的值检查输入。

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