如何解决此Python elif语句语法错误?

时间:2020-06-21 08:38:10

标签: python syntax-error

我试图编写一个计算器来用Python计算公式,但是我的elif语句出现语法错误。我在这里和其他站点上检查了其他几篇文章,但似乎人们犯的错误与我不同。谢谢您的帮助! :)这是我的代码:

# 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')

错误在这里:

# 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')

这是IDE所说的:

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

3 个答案:

答案 0 :(得分:0)

问题是if和elif子句之间的缩进代码:

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

hanswer = math.sqrt(secondsteph)

print (hanswer , "IS YOUR ANSWER")

input()
os.system('cls')

答案 1 :(得分:0)

if和elif必须具有相同的缩进级别:

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

您不能在这两者之间编写代码:

if <condition>:
    something

print()
variable = "something"

elif <condition>:
    somethingelse

然后您的代码将是:

# 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')

答案 2 :(得分:0)

您的用法是错误的,elif语句只能在if语句之后使用。

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

该部分仅在if语句中。 从secondsteph = (firstnh ** 2 + secondnh **2)行开始,它是一个新的代码块,而不是if块。如果这是缩进错误,请尝试:


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: "))

如果没有,请放置语句,

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

hanswer = math.sqrt(secondsteph)

print (hanswer , "IS YOUR ANSWER")

input()
os.system('cls')

elif块之后

相关问题