-3

i get the error traceback (most recent call last): File "main.py", line 64, in print(f" {num_1} {operation} {num_2} = {answer}") NameError: name 'answer' is not defined

idk why, tho. this code was working very well, so i didnt touch it. but as sson as i tried other mathematical operations it broke.

#inputs and stuff VV
num_1 = input("What's Your First Number?:  ")
operation = input(" + \n - \n * \n / \n Pick An Operation:  ")
num_2 = input("What's Your next Number?:  ")


#functions
def addition():
  answer = int(num_1) + int(num_2)

def subtraction():
  answer = int(num_1) - int(num_2)

def multiplication():
  answer = int(num_1) * int(num_2)

def division():
  answer = int(num_1) / int(num_2)

def continue_prog():
  print(answer)
  operation = input(" + \n - \n * \n / \n Pick An Operation:  ")
  new_num_2 = input("What's Your next Number?:  ")
  print(f" {answer} {operation} {new_num_2} = {answer}")

def Continue_or_not():
  continue_or_not = input(f"Typer 'Y' to Continue Calculating With {answer}. \n Or Type 'N' To   Begin New Operation \n Or 'C' To Close The Calculator: ")

  if continue_or_not == "Y":
    continue_prog()

  if continue_or_not == "N":
    clear()
    answer = 0
    num_1 = input("What's Your First Number?:  ")
    operation = input(" + \n - \n * \n / \n Pick An Operation:  ")
    num_2 = input("What's Your next Number?:  ")

  else:
    exit()
  
  
  
  

#computer checking which operation was picked
def chosen_operation():
  if operation == '+':
    addition()
  if operation == "-":
    subtraction()
  if operation == "*":
    multiplication()
  if operation == "/":
    division()

chosen_operation()


#Actual Code
print(f" {num_1} {operation} {num_2} = {answer}")
continue_or_not = input(f"Typer 'Y' to Continue Calculating With {answer}. \nOr Type 'N' To Begin New Operation \nOr 'C' To Close The Calculator: \n")

if continue_or_not == "Y":
  continue_prog()

if continue_or_not == "N":
  clear()

else:
  exit()

please help, i need this so i can impress my mother.

i tried deleted a variable i had, which was (answer = 0 ) so new variables can be put in the empty answer variable. but it broke the code more

2 Answers2

-1

In your code you define new 'answer' variable in each function again so the answer variable doesn't refer to the global 'answer' variable, to make the answer variable refer to the global one you can add global keyword like this

def addition():
  global answer 
  answer = int(num_1) + int(num_2)

def subtraction():
  global answer 
  answer = int(num_1) - int(num_2)

def multiplication():
  global answer 
  answer = int(num_1) * int(num_2)

def division():
  global answer 
  answer = int(num_1) / int(num_2)

and also you don't need to delete the answer = 0 variable in the global scope

Frederick
  • 14
  • 1
-1

main problem is your function is not returning any value. your are trying to print a local variable which is answer. so i change the code a bit so that it works. However, you can optimize the code further: #inputs and stuff VV

num_1 = input("What's Your First Number?:  ")
operation = input(" + \n - \n * \n / \n Pick An Operation:  ")
num_2 = input("What's Your next Number?:  ")


#functions
def addition():
  answer = int(num_1) + int(num_2)
  return answer

def subtraction():
  answer = int(num_1) - int(num_2)
  return answer

def multiplication():
  answer = int(num_1) * int(num_2)
  return answer

def division():
  answer = int(num_1) / int(num_2)
  return answer

def continue_prog():
  print(answer)
  operation = input(" + \n - \n * \n / \n Pick An Operation:  ")
  new_num_2 = input("What's Your next Number?:  ")
  print(f" {answer} {operation} {new_num_2} = {answer}")

def Continue_or_not():
  continue_or_not = input(f"Typer 'Y' to Continue Calculating With {answer}. \n Or Type 'N' To   Begin New Operation \n Or 'C' To Close The Calculator: ")

if continue_or_not == "Y":
continue_prog()

  if continue_or_not == "N":
    clear()
    answer = 0
    num_1 = input("What's Your First Number?:  ")
    operation = input(" + \n - \n * \n / \n Pick An Operation:  ")
    num_2 = input("What's Your next Number?:  ")

  else:
    exit()
  
  
  
  

#computer checking which operation was picked
def chosen_operation():
  if operation == '+':
    return addition()
  if operation == "-":
    return subtraction()
  if operation == "*":
    return multiplication()
  if operation == "/":
    return division()

answer = chosen_operation()


#Actual Code
print(f" {num_1} {operation} {num_2} = {answer}")
continue_or_not = input(f"Typer 'Y' to Continue Calculating With {answer}. \nOr Type 'N' To Begin New Operation \nOr 'C' To Close The Calculator: \n")

if continue_or_not == "Y":
  continue_prog()

else:
  print(f"Result:{answer}")