Calculator on Python

Question:

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

Asked By: Maks

||

Answers:

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 By: Barmar

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

Answered By: wateroverdose

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 By: mudlej
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.