简体   繁体   中英

Issue in the calculator

d = dict([(1,'Addition'),
          (2,'Substraction'),
          (3,'Multiplication'),
          (4,'Division(Integer)'),
          (5,'Division(Float)'),
          (6, 'Exponent')])
print(d)

n =int (input("Enter the Number whose operation u wanna perform :"))
a =int (input("Enter First Number :"))
b =int (input("Enter Second Number :"))
if n == 1:
    if 56 in a and 9 in b:
        print(77)
    else:
        print(a + b)
elif n==2:
     print(a - b)
elif n == 3:
    if 45 in a and 3 in b:
        print(555)
    else:
        print(a*b)
elif n == 4:
    print(a//b)
elif n == 5:
    if 56 in a and 6 in b:
        print(4)
    else:
        print(a/b)
elif n == 6:
    print(a**b)

Everything is fine but when I enter the number which is assigned like in addition, if I enter 56 and 9 in a and b respectively, I am getting this error:

 if 56 in a and 9 in b:
TypeError: argument of type 'int' is not iterable

You receive integers from input so you must compare using '==' instead of 'in'

if n == 1:
if 56 == a and 9 == b:
    print(77)
else:
    print(a + b)

Iterable means something that can be looped over - to go through one thing at a time until you reach the end. So you can ask if a list of items contains something by looking in the list for that item - which iterates until it's found.

Since you are asking if 56 is in a and it's not a list of items you've told Python to do something that doesn't make sense and it produces that error explaining why.

Now the use of == meaning is equal to is the correct answer as per other comments - however you could also have put a in a list with [a] - now in would work and == wouldn't since a list of one item a is not the same as a .

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