0

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 many numbers are you going to use?: 2
Please enter the number: 3
Please enter the number: 1
The answer is 4.

This is what I have so far:

print("Welcome to 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 would you like to use?:  "))

x = int(input("How many numbers would you like to use?:  "))

if operations == 1:
    a = 0
    sum = 0
    while a < x:
        number = int(input("Please enter number here:  "))
        a += 1
        sum = calculator.addition(number,sum)

I seriously need some help! All the tutorials on calculators in Python 3 are far more simple than this (as in it only take 2 numbers and then simply prints out the answer).

I need help getting the functions in the Calculator class I made to work. When I try running what I have so far, it lets me input my numbers and whatnot, but then it just ends. It doesn't run the operation or whatever. I am aware I only have addition so far, but if someone can just help me figure out addition, I think I can do the rest.

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
E. Kwak
  • 39
  • 3

2 Answers2

0

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.

Sam Hazleton
  • 470
  • 1
  • 5
  • 21
0

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!")

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 would you like to use?:  "))

x = int(input("How many numbers would you like to use?:  "))

if operations == 1:
    a = 0
    sum_ = 0
    while 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:  45
90
seralouk
  • 30,938
  • 9
  • 118
  • 133