Search code examples
pythonsyntax-error

How can I fix this Python elif statement syntax error?


I am trying to write a calculator to calculate a formula in Python, but I am getting a syntax error for my elif statement. I have checked on several other posts on here and other sites, but it seems people are making different mistakes than me. Thanks for your help in advance! :) Here is my code:

# IMPORTS

import os
import math

# SELECTION

print ("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
print ("~~~ PYTHAGOREAN THEOROM CALCULATOR ~~~")
print ("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
print ("OPTIONS:")
print ("1 - SOLVE FOR HYPOTENUSE")
print ("2 - SOLVE FOR LEG")
print ("3 - SOLVE FOR LEG 2")

user_choice = input("ENTER YOUR CHOICE: ")

# HYPOTENUSE MATHEMATICS

if user_choice == "1":
    firstnh = int(input("ENTER YOUR FIRST LEG'S DIMENSION: "))
    secondnh = int(input("ENTER YOUR SECOND LEG'S DIMENSION: "))

secondsteph = (firstnh ** 2 + secondnh **2)

hanswer = math.sqrt(secondsteph)

print (hanswer , "IS YOUR ANSWER")

input()
os.system('cls')

# LEG 1 MATHEMATICS

elif user_choice == "2":
    firstnl = int(input("ENTER YOUR LEG: "))
    secondnl = int(input("ENTER YOUR HYPOTENUSE: "))

secondstepl = (secondnl ** 2 - firstnl ** 2)

lanswer = math.sqrt(secondstepl)

print (lanswer, "IS YOUR ANSWER")

input()
os.system('cls')

The error is here:

# LEG 1 MATHEMATICS

elif user_choice == "2":  < - - - ERROR HERE
    firstnl = int(input("ENTER YOUR LEG: "))
    secondnl = int(input("ENTER YOUR HYPOTENUSE: "))

secondstepl = (secondnl ** 2 - firstnl ** 2)

lanswer = math.sqrt(secondstepl)

print (lanswer, "IS YOUR ANSWER")

input()
os.system('cls')

This is what the IDE says:

elif user_choice == "2":
    ^
SyntaxError: invalid syntax

Solution

  • Your usage is wrong, the elif statement must only be used after an if statement.

    if user_choice == "1":
        firstnh = int(input("ENTER YOUR FIRST LEG'S DIMENSION: "))
        secondnh = int(input("ENTER YOUR SECOND LEG'S DIMENSION: "))
    

    This part is only in the if statement. From the line secondsteph = (firstnh ** 2 + secondnh **2) onward, it is a new block of code, not in the if block. If this is an indentation error, Try :

    
    if user_choice == "1":
        firstnh = int(input("ENTER YOUR FIRST LEG'S DIMENSION: "))
        secondnh = int(input("ENTER YOUR SECOND LEG'S DIMENSION: "))
    
        secondsteph = (firstnh ** 2 + secondnh **2)
    
        hanswer = math.sqrt(secondsteph)
    
        print (hanswer , "IS YOUR ANSWER")
    
        input()
        os.system('cls')
    
    # LEG 1 MATHEMATICS
    
    elif user_choice == "2":
        firstnl = int(input("ENTER YOUR LEG: "))
        secondnl = int(input("ENTER YOUR HYPOTENUSE: "))
    

    If not, place the statements,

    secondsteph = (firstnh ** 2 + secondnh **2)
    
    hanswer = math.sqrt(secondsteph)
    
    print (hanswer , "IS YOUR ANSWER")
    
    input()
    os.system('cls')
    

    after the elif block