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
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')
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')
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.