I’m trying to make a very simple calculator in python. I’ve made a working one before using only functions, but adding classes is proving to be hard.
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()
Code runs fine but it gives me an “error” instead of outputting a calculation:
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:
Why would that be? Any help would be great, thanks.
You are printing the function itself, not the result of calling it. Try:
Or cleaner but more advanced: