我写了一个Python程序,它忽略了if循环。 有人可以告诉我我做错了什么吗? 请参见下面的代码:
class Check():
def __init__(self, op1, op2, op):
self.op1 = op1
self.op2 = op2
self.op = op
def main():
print("Enter first number: ")
num1 = int(input())
print("Enter second number: ")
num2 = int(input())
print("Enter operation: ")
op=str(input())
if op == '+':
return op1 + op2
elif op == '-':
return op1 - op2
elif op == '*':
return op1 * op2
elif op == '/':
return op1 / op2
if __name__ == "__main__":
main()
答案 0 :(得分:1)
问题是op1和op2都没有,只有self.op1,self.op2,num1和num2。我假设您要添加num1和num2。你也没放
self
在奶酪中,这是工作程序
class Check():
def __init__(self, op1, op2, op):
self.op1 = op1
self.op2 = op2
self.op = op
def cheese(self):
print("Enter first number: ")
op1 = int(input())
print("Enter second number: ")
op2 = int(input())
print("Enter operation: ")
op=str(input())
if op == '+':
return op1 + op2
elif op == '-':
return op1 - op2
elif op == '*':
return op1 * op2
elif op == '/':
return op1 / op2
a = Check(1,2,"+")
print(a.cheese())