繁体   English   中英

如何修复此 Python elif 语句语法错误?

[英]How can I fix this Python elif statement 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

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

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

hanswer = math.sqrt(secondsteph)

print (hanswer , "IS YOUR ANSWER")

input()
os.system('cls')

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

您的用法是错误的, 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块之后

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM