繁体   English   中英

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

[英]How to get the specific multiple value out from a list in python and If the user input is equal to the mulitple values print output

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

Output 我得到:

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

Output 我预计:

Progress

在上次 elif 评估中,您使用的是Pass[0] ,它不是列表而是值。 你应该写

elif Pass_Input == Pass[0]

Pass[5:6]是[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 output

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM