如果..elif测试可用于测试数字

时间:2016-01-27 18:49:45

标签: python if-statement

我的任务是:

  

您被要求编写一个程序,该程序将提供一个名称   形状取决于边数。用户只能输入   3到8之间的数字,如果他们输入任何其他数字,那么   程序应告诉他们输入3到8之间的数字。

这是我的Python回答:

#Sides and shapes
sides = int(input("How many sides on the shape are there? "))
if sides ==3:
    print ("Your shape is the triangle")
if sides ==4:
    print ("Your shape is the square")
if sides ==5:
    print ("Your shape is the pentagon")
if sides ==6:
    print ("Your shape is the hexagon")
if sides ==7:
    print ("Your shape is the heptagon")
if sides ==8:
    print ("Your shape is the octagon")
elif sides != range(3,9):
    print ("You should enter a number between 3 and 8")

elif语句后,我还需要循环它,以便如果该人输入的不是3-8,那么它将继续要求他们输入3到8之间的数字。

elif语句由于某种原因不起作用,并在F5中输出此答案:

How many sides on the shape are there? 6
Your shape is the hexagon
You should enter a number between 3 and 8

6 个答案:

答案 0 :(得分:3)

range()生成不同类型的对象; integer != range() 总是将成为现实。

测试整数是否超出<<=并且链接的范围:

elif not (3 <= sides < 9):
    print ("You should enter a number between 3 and 8")

或使用not in查看该数字是否超出范围:

elif sides not in range(3, 9):
    print ("You should enter a number between 3 and 8")

或者只是在所有您的测试中使用elif但是第一个,else用于最后一个分支;只有在if..elif测试不匹配时才会选择它:

if sides ==3:
    print ("Your shape is the triangle")
elif sides ==4:
    print ("Your shape is the square")
elif sides ==5:
    print ("Your shape is the pentagon")
elif sides ==6:
    print ("Your shape is the hexagon")
elif sides ==7:
    print ("Your shape is the heptagon")
elif sides ==8:
    print ("Your shape is the octagon")
else:
    print ("You should enter a number between 3 and 8")

请注意,现在只有一个 if;从逻辑上讲,elifelse部分属于那个if语句。任何其他if形成一个单独的选项集,并且您的sides != range(3, 9)表达式始终为true,这意味着elif测试在任何时候都是真的{{ 1}}不是真的。

您可以使用dictionary来简化代码。它允许您将键与值相关联;将数字作为密钥,您可以简单地测试字典中是否有if slides == 8,如果不是,则返回默认值

sides

此处,dict.get() method返回给定键的值,如果键不存在则返回默认值。

如果您需要继续循环,请根据以下内容测试密钥和分支的存在:

shape_msg = "Your shape is the "
result = {
    3: shape_msg + "triangle",
    4: shape_msg + "square",
    5: shape_msg + "pentagon",
    6: shape_msg + "hexagon",
    7: shape_msg + "heptagon",
    8: shape_msg + "octagon",
}

sides = int(input("How many sides on the shape are there? "))
result = results.get(sides, "You should enter a number between 3 and 8")
print(result)

有关如何询问用户输入和处理错误输入的更多提示,请参阅Asking the user for input until they give a valid response

答案 1 :(得分:1)

您的elif只是&#34;关联&#34;使用之前的if,因此对于任何不是八边形的东西,它会触及elif,然后其他人注意到如果你处于正确的范围内,你的比较没有测试,你&# 39;将int与列表进行比较。

可能你真的希望你的所有if语句除了第一个elif和你拥有elif的语句都是else条款块。

更好的是,您可以使用字典来映射邮件的边数,如果您没有字典中的密钥,则可以打印错误消息。

对于循环,如果你将整个事物放在while True中,你可以在每次成功后break而不是在无效的边数之后,这会导致它重复循环,直到它们最终进入有效号码。

答案 2 :(得分:0)

range(x, y)输出xy之间的一系列数字。然后,您将整数(这是边的值)与列表进行比较。显然它不等于列表,因为它是一个整数!

答案 3 :(得分:0)

请查看上一个if 6!= [3,4,5,6,7,8],以便您的代码输入elif语句并执行print ("You should enter a number between 3 and 8")

答案 4 :(得分:0)

您已经使用ifelif声明涵盖了所有法律答案,因此无需对其他值进行任何具体检查。

只需将最后一个语句更改为else

else:
    print ("You should enter a number between 3 and 8")

答案 5 :(得分:0)

我会做这样的事情(似乎有效):

invalidAnswer = True
while invalidAnswer :
    invalidAnswer = False
    sides = int(input("How many sides on the shape are there? "))
    if sides ==3:
        print ("Your shape is the triangle")
    elif sides ==4:
        print ("Your shape is the square")
    elif sides ==5:
        print ("Your shape is the pentagon")
    elif sides ==6:
        print ("Your shape is the hexagon")
    elif sides ==7:
        print ("Your shape is the heptagon")
    elif sides ==8:
        print ("Your shape is the octagon")
    else :
        print ("You should enter a number between 3 and 8")
        invalidAnswer = True