简体   繁体   English

计算器中的问题

[英]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:一切都很好,但是当我输入另外分配的数字时,如果我分别在ab中输入 56 和 9,我会收到此错误:

 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'您从输入中接收整数,因此您必须使用 '==' 而不是 '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.可迭代的意思是可以循环的东西 - 一次通过一件事到 go 直到你到达终点。 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.因此,您可以通过查看in项目的列表来询问项目列表是否包含某些内容 - 迭代直到找到它。

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.由于您要询问 56 是否in a中,并且它不是您告诉 Python 做一些没有意义的事情的项目列表,它会产生解释原因的错误。

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 .现在使用==含义于是根据其他评论的正确答案-但是您也可以将a放入带有[a]的列表中-现在in可以工作,而==不会因为一个项目的列表aa不同。

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

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