我的拆分功能不起作用

2024-10-03 21:33:05 发布

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

它给了我分析错误的机会。我不知道如何修复那个分裂函数。请帮忙。非常感谢。你知道吗

#Is It An Equilateral Triangle?
def equiTri():
    print("Enter three measure of a triangle: ")
    a, b, c = input().split()

    a = float(a)
    b = float(b)
    c = float(c)

    if a == b == c:
        print("You have an Equilateral Triangle.")
    elif a != b != c:
        print("This is a Scalene Triangle not an Equilateral Triangle.")
    else:
        print("""I'm guessing this is an Isosceles Triangle so definitely 
not
                an Equilateral Triangle.""")

Tags: 函数anisdef错误notitfloat
1条回答
网友
1楼 · 发布于 2024-10-03 21:33:05

这个错误发生在Python2中(您应该说明是哪个版本),并且只有当input()提示您输入三元组数字,但您在完全空白的行(除了空格)上按“回车”时才会发生。你知道吗

您可以使用以下方法处理该错误情况:

try:
    a,b,c = input("Enter three measures of a triangle: ")
else:
    raise RuntimeError("expected you to type three numbers separated by whitespace")

(使用try...catch并假设输入解析代码可以工作(EAFP:请求原谅比请求许可更容易),比在尝试拆分输入前后添加额外的行来测试输入更具python性和简洁性,这被视为“三思而后行”代码。)

相关问题 更多 >