我正在尝试用python制作一个非常简单的计算器。在只使用函数之前,我已经做了一个工作,但事实证明添加类很困难。
def askuser():
global Question, x, y
Question = input("""Enter a word: ("Add", "Subtract", "Multiply", "Divise")""")
x = int(input("Enter first number: "))
y = int(input("Enter second number: "))
class calculating:
def __init__(self, x, y):
self.x = x
self.y = y
def add(self):
return self.x + self.y
def subtract(self):
return self.x - self.y
def multiplication(self):
return self.x * self.y
def division(self):
return self.x / self.y
math = calculating
def calc():
if Question == "Add":
t = math.add
print(t)
elif Question == "Subtract":
t = math.subtract
print(t)
elif Question == "Multiply":
t = math.multiplication
print(t)
elif Question == "Division":
t = math.division
print(t)
def final():
input("Press any key to exit:" )
def main():
askuser()
calc()
final()
main()
代码运行良好,但它给了我一个“错误”而不是输出一个计算:
Enter a word: ("Add", "Subtract", "Multiply", "Divise")Add
Enter first number: 5
Enter second number: 5
function add at 0x02E4EC90
Press any key to exit:
为什么会这样?任何帮助都会很棒,谢谢。