0

I'm just starting to learn python, and recently I tried to create my own kinda calculator. I wrote the code, and everything looks pretty fine to me, but for some reason I get the error 'if math_multiply: NameError: name 'math_multiply' is not defined'. I get it whenever I try to do anything, but multiplying. Please help me with this. Attaching code to the message as well.

P.S.: I know this question is very stupid, but I am a newbie who's only starting... So, please pardon me.

f_n = input('Type in your first number: ')
s_n = input('Type in your second number: ')
math = input('What are we doing? ')
if math == '*':
 math_multiply = True
if math == '+':
 math_plus = True
elif math == '-':
 math_minus = True
elif math == '/':
 math_divide = True
if math_multiply:
 print(f'Your answer is: {int(f_n) * int(s_n)}')
elif math_plus:
 print(f'Your answer is: {int(f_n) + int(s_n)}')
elif math_minus:
 print(f'Your answer is: {int(f_n) - int(s_n)}')
elif math_divide:
 print(f'Your answer is: {int(f_n) / int(s_n)}')
else:
 print("It's not an arithmetic operation")
Penny Liu
18k5 gold badges89 silver badges109 bronze badges
asked Aug 20, 2022 at 18:02
2
  • Because the first setting of those math_* variables is happening inside of a conditional statement, then python tosses an error. It's worried that when it hits if math_multiply: math_multiply variable won't even exist. So just set each of those variables to nothing or false before doing your if statements. Like math_multiply=False, math_plus=False, math_minus=False, and math_divide=False. Commented Aug 20, 2022 at 18:07
  • 1
    BTW, your question isn't stupid. Most languages require that you declare your variables first, stating their name and their type which avoids confusing errors like this. Python doesn't care about all of that though and just requires that you initiate the variable, just not conditionally. Commented Aug 20, 2022 at 18:11

3 Answers 3

5

You only set math_multiply if they entered *, it's not set if they chose a different operation, so you get an error when you check its value. You should initialize all those variables to False so that the later tests will work.

f_n = input('Type in your first number: ')
s_n = input('Type in your second number: ')
math = input('What are we doing? ')
math_multiply = math_plus = math_minus = math_divide = False
if math == '*':
 math_multiply = True
elif math == '+':
 math_plus = True
elif math == '-':
 math_minus = True
elif math == '/':
 math_divide = True
if math_multiply:
 print(f'Your answer is: {int(f_n) * int(s_n)}')
elif math_plus:
 print(f'Your answer is: {int(f_n) + int(s_n)}')
elif math_minus:
 print(f'Your answer is: {int(f_n) - int(s_n)}')
elif math_divide:
 print(f'Your answer is: {int(f_n) / int(s_n)}')
else:
 print("It's not an arithmetic operation")

Alternatively you can do this without any of those variables. Just put the calculations in the first set of if statements:

f_n = input('Type in your first number: ')
s_n = input('Type in your second number: ')
math = input('What are we doing? ')
if math == '*':
 print(f'Your answer is: {int(f_n) * int(s_n)}')
elif math == '+':
 print(f'Your answer is: {int(f_n) + int(s_n)}')
elif math == '-':
 print(f'Your answer is: {int(f_n) - int(s_n)}')
elif math == '/':
 rint(f'Your answer is: {int(f_n) / int(s_n)}')
else:
 print("It's not an arithmetic operation")
answered Aug 20, 2022 at 18:07
Sign up to request clarification or add additional context in comments.

Comments

0

You didn't define the "math_multiply" variable. At the start of your code, add math_multiply = False

answered Aug 20, 2022 at 18:08

Comments

0

Previous answers explained the error. But I see that you are trying to use labels for operations, if so, you can check my version of your code.

# programs constant values for arithmetic operations
ADDITION, SUBTRACT = '+', '-'
MULTIPLY, DIVISION = '*', '/'
# taking input from the user
left_num = float(input('Type in your first number: '))
right_num = float(input('Type in your second number: '))
math_oper = input('What are we doing? ')
# calculating the result
if math_oper == ADDITION:
 result = left_num + right_num
elif math_oper == SUBTRACT:
 result = left_num - right_num
elif math_oper== MULTIPLY:
 result = left_num * right_num
elif math_oper == DIVISION:
 result = left_num / right_num
else:
 print("It's not an arithmetic operation")
 exit() # stop the program
# print the result
print(f"{left_num} {math_oper} {right_num} = {result}")

But notice that the program doesn't check the input thoroughly. It will crash if you, for example, try to divide by zero.

answered Aug 20, 2022 at 18:26

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.