-1

I decided to start learning python recently and I've been watching a youtube course to get introduced to the language. I decided to try and make a simple calculator that can add, subtract, multiply, and divide multiple numbers in order to practice what I learned so far.

Here's the code for the calculator:

num1 = float(input("Enter a number: "))
operation = input("Enter an operation (+,-,*,/): ")
if operation != "+" and operation != "-" and operation != "*" and operation != "/":
    print("Invalid Operator")
    exit()
num2 = float(input("Enter another number: "))
def add(num1,num2):
     global num1
     num1 += num2
     return num1
def subtract(num1,num2):
    num1 -= num2
    return num1
def multiply(num1,num2):
    num1 *= num2
    return num1
def divide(num1,num2):
    num1 /= num2
    return num1
while operation == "+" or operation == "-" or operation == "*" or operation == "/" or operation == "=":
    if operation == "+":
        result = add(num1,num2)
    elif operation == "-":
        result = subtract(num1,num2)
    elif operation == "*":
        result = multiply(num1,num2)
    elif operation == "/":
        result = divide(num1,num2)
    elif operation == "=":
        print(result)
        break
    if operation != "+" and operation != "=" and operation != "-" and operation != "*" and operation != "/":
        print("Invalid Operator")
        exit()
    operation = input("Enter an operation (+,-,*,/, or =): ")
    if operation == "=":
        print("Answer: " + str(result))
        break
    num2 = float(input("Enter another number: "))
print(num1)

However, my issue is that the variable "num1" isn't getting assigned a new value through assignment operators. The way I planned for this to work was for num1 to keep getting assigned the sum, difference, multiple, or quotient, of the previous two numbers depending on what operation the user wanted (ex. adding, subtracting, etc.) and how many numbers the user wanted to add, subtract, etc. Instead, it remains the value that the user inputs.

I included the print(num1) statement at the end of my code just to see what number num1 takes when I'm finished running the program.

Here's an example of what happens when I run the program.

Enter a number: 2
Enter an operation (+,-,*,/): +
Enter another number: 2
Enter an operation (+,-,*,/, or =): +
Enter another number: 2
Enter an operation (+,-,*,/, or =): =
Answer: 4.0
2.0
jrry
  • 3
  • 2
  • 5
    Hi, welcome to Stack Overflow, can please you post your code as text instead of an image? Here's why - [Why not upload images of code/errors when asking a question?](https://meta.stackoverflow.com/q/285551) – Nick Parsons Jul 04 '21 at 01:31
  • 2
    Does this answer your question? [Why can a function modify some arguments as perceived by the caller, but not others?](https://stackoverflow.com/questions/575196/why-can-a-function-modify-some-arguments-as-perceived-by-the-caller-but-not-oth) – John Kugelman Jul 04 '21 at 01:37
  • 2
    Or this? [How do I pass a variable by reference?](https://stackoverflow.com/questions/986006/how-do-i-pass-a-variable-by-reference/986145) – John Kugelman Jul 04 '21 at 01:38
  • 1
    ..and it's not `num1` that you should be tracking, but `result`. You always return the result of the operation and store it in `result`. – Gino Mempin Jul 04 '21 at 01:47
  • The simplest answer is: you are passing `num1` to functions as a parameter but the `num1` you are printing is `global` – Derek Jul 04 '21 at 02:25

1 Answers1

0

Python has scopes, it means when you create a variable at outer level than your scope, you can’t modify that variable within inner scope unless you ‘globalize’ it. for example:

x = 0

def increment():
    x = x + 1 # same as x += 1
    print(x)

increment()
print(x)

It will print 1 then 0, instead of 1 and 1. Because it only got changed in the function scope and not outside. In order to affect outer variables from our functions we can use the keyword global.


x = 0

def increment():
    global x
    x += 1
    print(x)
increment()
print(x)

Now it will print 1 in both times because global made it possible to modify the variable even for outer scopes.

Jonathan1609
  • 1,809
  • 1
  • 5
  • 22