Python,在输入中运行时出现Elif错误

时间:2018-10-01 23:56:49

标签: python

每次运行此代码时,我都会在中间的elif上收到错误消息,并尝试通过调整间距等来解决此问题。

print("Do you want to find the area of a triangle or trapezoid?")
shape = input()

if shape = "Triangle" or "triangle":
    height = float(input("Please enter the height of the triangle: "))
    base = float(input("Please enter the base length of the triangle: "))
    area = 0.5 * height * base
    print("The area of a triangle with height", height, "and base", base, "is", area, ".")
  elif shape = "Trapezoid" or "trapezoid":
      height_1 = float(input("Please enter the Height of the trapezoid: "))
      base_1 = float(input('Please enter the base one value: '))
      base_2 = float(input('Please enter the base two value: '))
      area_1 = ((base_1 + base_2) / 2) * height_1 #This line is the math for figuring the area of the triangle
    print("The area of the Trapezoid height", height_1, "and bases", base_1, "and", base_2, "is", area_1, ".")
  else:
    print("Wrong shape or misspelled shape. Please check it again!")

4 个答案:

答案 0 :(得分:2)

您必须对==lower

print("Do you want to find the area of a triangle or trapezoid?")
shape = input()

if shape.lower() == "triangle":
    height = float(input("Please enter the height of the triangle: "))
    base = float(input("Please enter the base length of the triangle: "))
    area = 0.5 * height * base
    print("The area of a triangle with height", height, "and base", base, "is", area, ".")
elif shape.lower() == "trapezoid":
    height_1 = float(input("Please enter the Height of the trapezoid: "))
    base_1 = float(input('Please enter the base one value: '))
    base_2 = float(input('Please enter the base two value: '))
    area_1 = ((base_1 + base_2) / 2) * height_1 #This line is the math for figuring the area of the triangle
    print("The area of the Trapezoid height", height_1, "and bases", base_1, "and", base_2, "is", area_1, ".")
else:
    print("Wrong shape or misspelled shape. Please check it again!")

答案 1 :(得分:2)

我建议您清除代码。 首先,您需要了解'='运算符和'=='运算符之间的区别。第一个执行作业:

a = "Hello World"
b = 7

在python中,这意味着a是指向包含字符串值'Hello World'的存储区的指针,b是指向包含int值'7'的存储区的指针。 Python是强力但具有动态类型的类型,我不建议您开始使用这种语言进行编程,的确,它具有很多功能,您将成为专家! 运算符'=='是2个值之间的布尔运算符,在数学上,我们可以说'=='将来自相同域(它们必须具有相同类型!)的两个值映射到{TRUE,FALSE}域。这是一个布尔表达式,当且仅当您提供的2个值相等时才返回TRUE。这里我们遇到了第一个python问题,因为它与教学背道而驰。实际上:

7 == 7 #True!
a = 7
a == 7 #True!
a = "Hello World"
a == "Hello World" #True!

7和7都是整数值,并且它们是相同的值,所以是的,确实7等于7!但是,如果a是一个指针,而7是一个值,则确实a == 7而a是一个Pointer,而7是一个值!更深入。如果a =“ Hello World”在python中为true,则==“ Hello World”,但在数学上则不正确(因为域和“ Hello World”域不同),因此不正确用其他所有OOP语言(例如Java或C ++)表示,因为“ a”和“ Hello World”是表示相同值的不同对象!

现在很清楚'='和'=='运算符之间的区别,我们可以讨论您的问题。在编程中,最好将字符串声明为“先验”,然后在代码中的任意位置调用它们。这将导致代码更具可读性和可维护性(修改一次,到处修改)。

ask = "Do you want to find the area of a triangle or trapezoid?\n"
askheight = "Plase enter the height of the triangle: "
askbase = "Please enter the base length of the triangle: "
asktrapheight = "Please enter the height of the trapezoid: "
askminbase = "Plase enter the minor base value: "
askmajbase = "Plase enter the major base value: "
areas = "The area of the shape is: "
ILLEGAL_ARGUMENT_EXCEPTION = "Wrong shape or misspelled shape. Please check it again!"

然后输入。您想读取字符串,而不是整数。因此,您需要的功能是“ raw_input()”。如果您的程序不需要区分大小写,我建议您也将输入转换为小写并在代码中仅管理小写字符串:

shape = raw_input(ask)
shape = shape.lower()

这时,您想检查用户输入的有效性,即达到鲁棒性的好方法!为了始终管理“运行时异常”,请始终首先执行此操作。定义此功能:

def illegalArgument(shape):
    if shape == "triangle" or shape == "trapezoid":
        return False
    return True

在输入后调用它:

if illegalArgument(shape):
    raise Exception(ILLEGAL_ARGUMENT_EXCEPTION)

现在您可以执行任务了:

if shape == "triangle":
    height = float(input(askheight))
    base = float(input(askbase))
    area = 0.5*height*base
else:
    height = float(input(asktrapheight))
    basemin = float(input(askminbase))
    basemaj = float(input(askmajbase))
    area = ((basemin+basemaj)/2)*height
print(area+str(area))

还请注意:

print(areas+str(area))

字符串之间的'+'运算符表示您正在连接两个字符串。确实:

"Hello"+"World" #it is "HelloWorld"

但是我们将'area'作为字符串,并将'area'作为整数!因此,字符串和整数之间的'+'运算符不会减少为一个值,因为它在运行时是非法操作,并且您的程序将崩溃。函数'str(area)'将整数值转换为字符串,并且'+'操作是类型安全的。

这是完整的代码:

def illegalArgument(shape):
    if shape == "triangle" or shape == "trapezoid":
        return False
    return True
if __name__ == '__main__': #Just another python good practice!
    ask = "Do you want to find the area of a triangle or trapezoid?\n"
    askheight = "Plase enter the height of the triangle: "
    askbase = "Please enter the base length of the triangle: "
    asktrapheight = "Please enter the height of the trapezoid: "
    askminbase = "Plase enter the minor base value: "
    askmajbase = "Plase enter the major base value: "
    areas = "The area of the shape is: "
    ILLEGAL_ARGUMENT_EXCEPTION = "Wrong shape or misspelled shape. Please check it again!"
    shape = raw_input(ask)
    shape = shape.lower()
    if illegalArgument(shape):
        raise Exception(ILLEGAL_ARGUMENT_EXCEPTION)
    if shape == "triangle":
        height = float(input(askheight))
        base = float(input(askbase))
        area = 0.5*height*base
    else:
        height = float(input(asktrapheight))
        basemin = float(input(askminbase))
        basemaj = float(input(askmajbase))
        area = ((basemin+basemaj)/2)*height
    print(areas+str(area))

祝你学习顺利!

答案 2 :(得分:0)

在python中比较两个值时,您使用的是==,而不是=

=是分配运算符,不是比较运算符。

此外,您不能执行x == y or z。您必须做x == y or x == z

因此,您的if语句将变为:

if shape == "Triangle" or shape == "triangle":

,您的elif将变为:

elif shape == "Trapezoid" or shape =="trapezoid":

最后,您的缩进是错误的,这就是谈论python条件语句的全部内容。

这是您最终代码的外观:

print("Do you want to find the area of a triangle or trapezoid?")
shape = input()

if shape == "Triangle" or shape == "triangle":
    height = float(input("Please enter the height of the triangle: "))
    base = float(input("Please enter the base length of the triangle: "))
    area = 0.5 * height * base
    print("The area of a triangle with height", height, "and base", base, "is", area, ".")
elif shape == "Trapezoid" or shape == "trapezoid":
    height_1 = float(input("Please enter the Height of the trapezoid: "))
    base_1 = float(input('Please enter the base one value: '))
    base_2 = float(input('Please enter the base two value: '))
    area_1 = ((base_1 + base_2) / 2) * height_1 #This line is the math for figuring the area of the triangle
    print("The area of the Trapezoid height", height_1, "and bases", base_1, "and", base_2, "is", area_1, ".")
else:
    print("Wrong shape or misspelled shape. Please check it again!")

除此之外,您的代码及其效率还有许多其他问题。例如,您无法在print语句中将字符串连接起来。建议您阅读the following string concatenation tutorial,以了解如何解决现有问题。

答案 3 :(得分:0)

elif上的缩进不可用,并且还有其他一些错误:

  • 对于 等于 ,您应该使用==
  • 对于每个需要满足您的if的条件,您需要输入 整个过程,而不仅仅是使用or运算符
  • 您将无法打印类型float,因此请转换为 打印

尝试一下:

print("Do you want to find the area of a triangle or trapezoid?")
shape = input()

if shape == "Triangle" or shape == "triangle":
    height = float(input("Please enter the height of the triangle: "))
    base = float(input("Please enter the base length of the triangle: "))
    area = 0.5 * height * base
    print("The area of a triangle with height "+ str(height) +" and base "+ str(base) +" is "+ str(area) +".")
elif shape == "Trapezoid" or shape == "trapezoid":
    height_1 = float(input("Please enter the Height of the trapezoid: "))
    base_1 = float(input('Please enter the base one value: '))
    base_2 = float(input('Please enter the base two value: '))
    area_1 = ((base_1 + base_2) / 2) * height_1 #This line is the math for figuring the area of the triangle
    print("The area of the Trapezoid height "+ str(height_1) +" and bases "+ str(base_1) +" and "+ str(base_2) +" is "+ str(area_1) +".")
else:
    print("Wrong shape or misspelled shape. Please check it again!")

:如果您不想仅使用以下命令来检查upperlowercase

if shape.lower() == "triangle":