Please i cant understand why this code doesn't run when I choose 2(-) as my operator but instead gives a 'num1' not defined | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Please i cant understand why this code doesn't run when I choose 2(-) as my operator but instead gives a 'num1' not defined

#This is a simple calculator I'm trying to program as my first project in python import sys while True: print('The choices/operations available are:') print('type 1 for addition (+)') print('type 2 for substraction (-)') print('type 3 for multiplication (*)') print('type 4 for division (/)') print('type 5 for square root (^2)') print('type 6 for remainder (%)') print('type 0 to close (exit)') choice=int(input('enter ur choice:')) if choice==0: sys.exit elif choice==1: num1=float(input('enter first number: ')) num2=float(input('enter second number:')) result=num1 + num2 print(result) if choice==2: num1=float(input('enter first number:')) num2=float(input('enter second number:')) result=num1 - num2 print(result)

4th Jul 2020, 10:53 AM
Ibrahim Phenom
2 Answers
+ 1
# Hint: Why not put the number inputs outside then write your conditional statements. Like this: choice = input('enter your choice') num1 = float(input()) num2 = float(input()) If choice == this: do that elif choice == that: do this...... This way you won't have to repeat yourself 😃😃 Hope it helps.
4th Jul 2020, 11:46 AM
Tomiwa Joseph
Tomiwa Joseph - avatar
+ 1
Thanks
4th Jul 2020, 1:04 PM
Ibrahim Phenom