Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Calculator on Python

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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")
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading