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

Question:

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")

Input:

Enter : 120

Output I get:

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

Output I expect:

Progress
Asked By: Anjana Dep

||

Answers:

In your last elif evaluation, you are using Pass[0] which is not a list but a value. You should write

elif Pass_Input == Pass[0]
Answered By: dev_master089

Pass[5:6] is [100]
and 120 is not in [100] with no doubt.

list slice Pass[5:6] means from 5 to 6, which 5 is included and 6 is not.

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

then the program runs to elif Pass_Input in Pass[0]: . Pass[0] is 0, which can not be iterable, so you get a TypeError


you can change Pass[5:6] to Pass[5:7] to get Process output

Answered By: 王毅钧
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.