-1

I have been trying to make this calculator work but it always says that my choice is invalid. I tried valid numbers and for some reason it doesn't work. Any help would be appreciated. I'm still new to python and to programming in general. Thanks.

#Calculator
print("1.Addition")
print("2.Substraction")
print("3.Multiplication")
print("4.Divison")
#Choose the calculation
Choice=int(input("Enter your choice (1/2/3/4): "))
#Inserting the numbers
Num1=float(input("Insert your first number: "))
Num2=float(input("Insert your second number: "))

if Choice == '1':
    ans= Num1 + Num2
    print("Your answer is: ",ans)

elif Choice == '2':
    ans= Num1 - Num2
    print("Your answer is: ",ans)

elif Choice == '3':
    ans= Num1*Num2
    print("Your answer is: ",ans)

elif Choice == '4':
    ans= Num1/Num2
    print("Your answer is: ",ans)

else:
    print("Invalid choice. ")
Stidgeon
  • 2,673
  • 8
  • 20
  • 28
IskandarG
  • 1
  • 1
  • 3

3 Answers3

2

Modify the if conditions, replacing the strings with the numbers themselves:

if Choice == 1:
    ans= Num1 + Num2
    print("Your answer is: ",ans)

elif Choice == 2:
    ans= Num1 - Num2
    print("Your answer is: ",ans)

elif Choice == 3:
    ans= Num1*Num2
    print("Your answer is: ",ans)

elif Choice == 4:
    ans= Num1/Num2
    print("Your answer is: ",ans)

else:
    print(f"Invalid choice.")
leqo
  • 356
  • 4
  • 15
1

the problem is your checking if the answer is equivalent to a STRING not an integer!

#Calculator
print("1.Addition")
print("2.Substraction")
print("3.Multiplication")
print("4.Divison")
#Choose the calculation
choice=int(input("Enter your choice (1/2/3/4): "))
#Inserting the numbers1
Num1=int(input("Insert your first number: "))
Num2=int(input("Insert your second number: "))

if choice == 1:
    ans= Num1 + Num2
    print("Your answer is: ",ans)
elif choice == 2:
    ans= Num1 - Num2
    print("Your answer is: ",ans)
elif choice == 3:
    ans= Num1*Num2
    print("Your answer is: ",ans)
elif choice == 4:
    ans= Num1/Num2
    print("Your answer is: ",ans)
else:
    print("Invalid choice. ")
Ironkey
  • 2,568
  • 1
  • 8
  • 30
0
Choice=int(input("Enter your choice (1/2/3/4): "))

Choice is an int.

if Choice == '1':

You are comparing it against a String('1'). So your if-statements will not work.

Try this:

if Choice == 1:
rdas
  • 20,604
  • 6
  • 33
  • 46