首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法获得简单的if..elif测试来处理数字测试

无法获得简单的if..elif测试来处理数字测试
EN

Stack Overflow用户
提问于 2016-01-27 18:49:44
回答 6查看 105关注 0票数 0

我的任务是:

您已经被要求编写一个程序,它将根据边数给出形状的名称。用户只能输入3到8之间的数字,如果他们输入任何其他数字,那么程序应该告诉他们输入一个介于3和8之间的数字。

这里是我的Python回答:

代码语言:javascript
复制
#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中输出这个答案:

代码语言:javascript
复制
How many sides on the shape are there? 6
Your shape is the hexagon
You should enter a number between 3 and 8
EN

回答 6

Stack Overflow用户

回答已采纳

发布于 2016-01-27 18:53:57

range()生成一个不同类型的对象;integer != range()总是为真。

要么用<<=测试整数是否超出了范围,然后链接:

代码语言:javascript
复制
elif not (3 <= sides < 9):
    print ("You should enter a number between 3 and 8")

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

代码语言:javascript
复制
elif sides not in range(3, 9):
    print ("You should enter a number between 3 and 8")

或者只需在所有测试中使用elif (第一个测试除外),对最后一个分支使用else;只有在if..elif测试不匹配的情况下才会选择if..elif测试:

代码语言:javascript
复制
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不为真的任何时候都是真的。

您可以使用https://docs.python.org/3/tutorial/datastructures.html#dictionaries简化代码。它允许您将键与值相关联;将数字作为键,您可以简单地测试字典中是否有sides,如果没有,则返回默认值:

代码语言:javascript
复制
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返回给定键的值,或者如果键不存在,则返回默认值。

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

代码语言:javascript
复制
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

票数 3
EN

Stack Overflow用户

发布于 2016-01-27 18:55:58

您的elif只与前一个if“关联”,因此它会对任何不是八角形的东西按下该elif,然后正如其他人注意到的那样,您的比较不是在测试您是否在正确的范围内,而是将一个int与一个列表进行比较。

可能您确实希望您的所有if语句都是elif语句,除了第一个语句是elif语句,还有一个elif语句是整个块的else子句。

更好的是,您可以有一个字典,它可以将边数映射到消息,如果字典中没有键,则可以打印错误消息。

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

票数 1
EN

Stack Overflow用户

发布于 2016-01-27 18:54:21

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35045617

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档