python - Calculator function outputs nothing -
i need design calculator following ui:
welcome calculator! 1 addition 2 subtraction 3 multiplication 4 division operation going use?: 1 how many numbers going use?: 2 please enter number: 3 please enter number: 1 answer 4. this have far:
print("welcome calculator!") class calculator: def addition(self,x,y): added = x + y return sum def subtraction(self,x,y): diff = x - y return diff def multiplication(self,x,y): prod = x * y return prod def division(self,x,y): quo = x / y return quo calculator = calculator() print("1 \taddition") print("2 \tsubtraction") print("3 \tmultiplication") print("4 \tdivision") operations = int(input("what operation use?: ")) x = int(input("how many numbers use?: ")) if operations == 1: = 0 sum = 0 while < x: number = int(input("please enter number here: ")) += 1 sum = calculator.addition(number,sum) i need help! tutorials on calculators in python 3 far more simple (as in take 2 numbers , prints out answer).
i need getting functions in calculator class made work. when try running have far, lets me input numbers , whatnot, ends. doesn't run operation or whatever. aware have addition far, if can me figure out addition, think can rest.
the code not work. in addition function return variable sum conflict build in sum function.
so return added , in general avoid sum, use sum_ :
this works fine me:
print("welcome calculator!") class calculator: def addition(self,x,y): added = x + y return added def subtraction(self,x,y): diff = x - y return diff def multiplication(self,x,y): prod = x * y return prod def division(self,x,y): quo = x / y return quo calculator = calculator() print("1 \taddition") print("2 \tsubtraction") print("3 \tmultiplication") print("4 \tdivision") operations = int(input("what operation use?: ")) x = int(input("how many numbers use?: ")) if operations == 1: = 0 sum_ = 0 while < x: number = int(input("please enter number here: ")) += 1 sum_ = calculator.addition(number,sum_) print(sum_) running:
$ python s.py welcome calculator! 1 addition 2 subtraction 3 multiplication 4 division operation use?: 1 how many numbers use?: 2 please enter number here: 45 please enter number here: 45 90
Comments
Post a Comment