如何在python中从列表中获得特定的多个值,如果用户输入等于多个值打印输出


Pass=[0,20,40,60,80,100,120]
while True:
Pass_Input=int(input("Enter : "))
if Pass_Input in Pass[5:6]:
print("Progress")
elif Pass_Input in Pass[0:2]:
print("Progress Module Trailer")
elif Pass_Input in Pass[0]:
print("Exclude")

输入:

Enter : 120

输出:

Traceback (most recent call last):
File "E:IITPythonCW_Python1 st question 2nd try.py", line 8, in <module>
elif Pass_Input in Pass[0]:
TypeError: argument of type 'int' is not iterable

期望输出:

Progress

Pass[5:6] is [100]毫无疑问,120不在[100]之内。

list slice Pass[5:6]表示从5到6,其中5包括在内,6不包括在内。

In [1]: Pass=[0,20,40,60,80,100,120]
In [2]: Pass[5:6]
Out[2]: [100]

则程序运行到elif Pass_Input in Pass[0]:。Pass[0]是0,它不能迭代,所以你得到一个TypeError


您可以将Pass[5:6]更改为Pass[5:7]以获得Process输出

在最后一次elif求值中,您使用的是Pass[0]它不是一个列表,而是一个值。你应该写

elif Pass_Input == Pass[0]

最新更新