Can someone help me with this simple calculator program in python? I am having problem in finding error

Question:

Program got a Syntax error as follow:

elif choice == "3":
^^^^
SyntaxError: invalid syntax

print("1 Additionn2 Subtractionn3 Multiplicationn4 Division ")
choice= input ("WHat is you choice? : ")
num1 = float (input("Please enter a number: "))
num2 = float( input("please enter another number: "))

if choice == "1":
    print(Num1,"+", Num2, "=", (Num1 + Num2))
    elif choice == "2":
    print(Num1,"-", Num2, "=", (Num1 - Num2))
    elif choice == "3":
    print(Num1,"x", Num2, "=", (Num1 * Num2))
    elif choice == "4":
        if Num2 == 0.0
            print("0 error LOL")
        else:
            print(Num1, "/", Num2, "=", (Num1 / Num2) )
else:
    print("your choice is bad...")
    
Asked By: Slackedpew

||

Answers:

You need to unindent the elif statements like:

print("1 Additionn2 Subtractionn3 Multiplicationn4 Division ")
choice= input("What is you choice? : ")
num1 = float(input("Please enter a number: "))
num2 = float(input("please enter another number: "))

if choice == "1":
    print(f"{Num1}+{Num2}={Num1 + Num2}")
elif choice == "2":
    print(f"{Num1}-{Num2}={Num1 - Num2}")
elif choice == "3":
    print(f"{Num1}*{Num2}={Num1 * Num2}")
elif choice == "4":
    if Num2 == 0.0
        print("0 error LOL")
    else:
       print(f"{Num1}/{Num2}={Num1 / Num2}")
else:
    print("your choice is bad...")

I changed your code slightly and used f-strings to make it slightly neater (in my opinion)

Answered By: Prime Price

Variable names are case sensitive ("num1" cannot be referenced as "Num1")

Indentation on elif should be inline with the original "if"

Missing colon on if statement on line 13.

Here is an altered version that worked for me:

print("1 Additionn2 Subtractionn3 Multiplicationn4 Division ")
choice= input ("WHat is you choice? : ")
num1 = float (input("Please enter a number: "))
num2 = float( input("please enter another number: "))

if choice == "1":
    print(num1,"+", num2, "=", (num1 + num2))
elif choice == "2":
    print(num1,"-", num2, "=", (num1 - num2))
elif choice == "3":
    print(num1,"x", num2, "=", (num1 * num2))
elif choice == "4":
        if num2 == 0.0:
            print("0 error LOL")
        else:
            print(num1, "/", num2, "=", (num1 / num2) )
else:
    print("your choice is bad...")
Answered By: michael perkins
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.