如果..elif test与测试一起工作,那就太简单了

2024-04-26 03:48:35 发布

您现在位置:Python中文网/ 问答频道 /正文

我的任务是:

You have been asked to write a program that will give the name of a shape depending on the number of sides. The user can only enter numbers between 3 and 8, if they enter any other number then the program should tell them to enter a number between 3 and 8.

下面是我的答案:

#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

Tags: andtheyounumberyourifison
3条回答

range(x, y)输出介于xy之间的数字范围。然后将一个整数(即边的值)与一个列表进行比较。显然它不等于列表,因为它是一个整数!你知道吗

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)表达式总是正确的,这意味着elif测试在if slides == 8不正确的时候都是正确的。你知道吗

可以使用dictionary简化代码。它允许您将一个键与一个值相关联;将数字作为键,您可以简单地测试sides是否在字典中,或者返回一个默认值,如果不是:

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)

这里,^{} method返回给定键的值,如果键不存在,则返回默认值。你知道吗

如果需要继续循环,请基于以下内容测试是否存在密钥和分支:

while True:
    sides = int(input("How many sides on the shape are there? "))
    if sides in result:
        print(sides[result])
        break  # done, exit the loop
     print("You should enter a number between 3 and 8")

有关如何请求用户输入并处理错误输入的更多提示,请参见Asking the user for input until they give a valid response。你知道吗

您的elif只与前面的if关联,因此对于任何不是八角形的东西,它都会击中elif,然后正如其他人注意到的那样,您的比较并不是测试您是否在正确的范围内,而是将int与列表进行比较。你知道吗

可能您真的希望所有的if语句,除了第一个是elif,另一个作为elif的语句是整个块的else子句。你知道吗

更好的是,你可以有一个字典来映射消息的边数,如果字典中没有键,你可以打印错误消息。你知道吗

对于循环,如果你把整个东西放在while True中,你可以在每次成功之后break,而不是在无效的边数之后,这将导致循环重复,直到它们最终输入一个有效的数字。你知道吗

相关问题 更多 >