Calculator Function Outputs Nothing
I need to design a calculator with the following UI: Welcome to Calculator! 1 Addition 2 Subtraction 3 Multiplication 4 Division Which operation are you going to use?: 1 How ma
Solution 1:
The code as it is does not work. In the addition
function you return the variable sum
which will conflict with the build in sum
function.
So just return the added and in general avoid the sum
, use something like sum_
:
This works fine for me:
print("Welcome to Calculator!")
classCalculator:
defaddition(self,x,y):
added = x + y
return added
defsubtraction(self,x,y):
diff = x - y
return diff
defmultiplication(self,x,y):
prod = x * y
return prod
defdivision(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 would you like to use?: "))
x = int(input("How many numbers would you like to use?: "))
if operations == 1:
a = 0
sum_ = 0while a < x:
number = int(input("Please enter number here: "))
a += 1
sum_ = calculator.addition(number,sum_)
print(sum_)
Running:
$ python s.py
Welcome to Calculator!
1 Addition
2 Subtraction
3 Multiplication
4 Division
What operation would you like to use?: 1
How many numbers would you like to use?: 2
Please enter number here: 45
Please enter number here: 4590
Solution 2:
Your program takes input, runs a series of operations, and then ends without ever displaying the result. Try something like print(sum)
after the while loop ends.
Post a Comment for "Calculator Function Outputs Nothing"