它给了我eof解析错误。我不知道如何修复分割功能。请帮忙。谢谢。
#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.""")
答案 0 :(得分:1)
错误发生在Python 2中(您应该说明哪个版本),并且仅当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
更加Pythonic和更清洁,并假设输入解析代码有效(" EAFP:更容易请求宽恕而不是权限" ),而不是在尝试拆分输入之前和之后添加额外的行来测试输入,这被忽略为"在跳过" 代码之前先查看。)