-5

The problem is a calculator im trying to build and inside the define block is the result of my work but I cant print result outside of the def block because the result doesnt have value

I tried to import math and defined main(): and in the block I put every thing needed and outside the block I expected to give me the result but it gave me the error (UnboundLocalError: cannot access local variable 'result' where it is not associated with a value)

import math

def main():
# Prompt user for operation 
op = input("Enter the operation (+, -, *, /, % or ^): ")

# Prompt user for first number  
num1 = float(input("Enter the first number: "))  

# Prompt user for second number  
num2 = float(input("Enter the second number: "))  

if op == "+":  
        result = num1 + num2  
elif op == "-":
        result = num1 - num2
elif op == "*":
        result = num1 * num2
elif op == "/":
        result = num1 / num2
elif op == "%":
        result = num1 % num2
elif op == "^":
        result = pow(num1, num2)
else:
        print("Invalid operation")

# Print result
print("Result: ", result)

main()
Cow
  • 2,543
  • 4
  • 13
  • 25
  • 2
    Please share the code you have tried with, then we can both help you troubleshoot and better understand the issues you are having. Remember to add the full traceback. To learn more about how the [SO] community can help you, please take the [tour] and read [ask]. – Cow Aug 23 '23 at 11:10
  • What input are you entering? – Cow Aug 23 '23 at 11:22
  • 3
    That is the whole point of functions: functionality encapsulation. You cannot access the internals of a function from the outside. You must explicitly return the values you want to access outside with the `return` keyword. – Louis Lac Aug 23 '23 at 11:23
  • @Cow I don't get it – Zaker Samoray Aug 23 '23 at 11:23
  • @LouicLac I tried but it gave me a syntax error – Zaker Samoray Aug 23 '23 at 11:24
  • It cant return the value of the result I dont know why "UnboundLocalError: cannot access local variable 'result' where it is not associated with a value" – Zaker Samoray Aug 23 '23 at 11:28

1 Answers1

0

If you want the print("Result: ", result)outside of main and still access the calculated value you need the function main to return a value (mainly the result variable):

def main():
    # Prompt user for operation 
    op = input("Enter the operation (+, -, *, /, % or ^): ")

    # Prompt user for first number  
    num1 = float(input("Enter the first number: "))  

    # Prompt user for second number  
    num2 = float(input("Enter the second number: "))  

    if op == "+":  
        result = num1 + num2  
    elif op == "-":
        result = num1 - num2
    elif op == "*":
        result = num1 * num2
    elif op == "/":
        result = num1 / num2
    elif op == "%":
        result = num1 % num2
    elif op == "^":
        result = pow(num1, num2)
    else:
        print("Invalid operation")
    return result

res = main()
print(f"Eregbnis: {res}")

You do not need math imported then

Lenntror
  • 186
  • 10