How can I fix this Python elif statement syntax error?

Question:

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
Asked By: Nipar

||

Answers:

The problem is the unindented code between the if and the elif clauses:

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

hanswer = math.sqrt(secondsteph)

print (hanswer , "IS YOUR ANSWER")

input()
os.system('cls')
Answered By: Sergio Gracia

if and elif have to be in the same indentation level:

if <condition>:
    something
elif <condition>:
    somethingelse

You can’t write code in between like this:

if <condition>:
    something

print()
variable = "something"

elif <condition>:
    somethingelse

then your code will be:

# 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')
Answered By: EdKenbers

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

Answered By: Roshin Raphel

Hi i was having the same issue in Python 3 with the if and elif statements and new to programming too. This info has helped me to solve it so a huge Thank You for sharing your experience and knowledge.

Answered By: Niven Smith
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.