【问题标题】:My split function is not working我的拆分功能不起作用
【发布时间】:2018-02-04 23:50:21
【问题描述】:

它给了我 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.""")

【问题讨论】:

  • 无法重现您的错误。它可能来自代码中的其他地方。您给定的代码也没有正确缩进。
  • 修正原始缩进后同样无法重现错误。
  • 您输入的内容是什么导致出现此错误?
  • 我的输入是 5, 5, 5。我也试试 5 5 5。
  • 我现在知道错误了。我在 3 中完成了代码。我放入了谷歌驱动器。回家。当然,在 2 中打开它。谢谢大家。

标签: python input python-2.x eof


【解决方案1】:

该错误发生在 Python 2 中(您应该已经说明了哪个版本),并且仅当 input() 提示您输入三个数字但您在完全空白的行(空格除外)上按“Enter”时才会发生此错误.

您可以通过以下方式处理该错误情况:

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

(与添加额外的行来测试尝试拆分之前和之后的输入,这被视为“在你跳跃之前查看”代码。)

【讨论】:

  • Python 2 中的错误是正确的。我只是注意到了。谢谢。我的代码是在 3 中编写的。
  • 如果您不小心在 Python 2 中运行,如果您想检测并停止并显示明确消息,另请参阅 How to detect Python Version 2 or 3 in script?
猜你喜欢
  • 2014-03-05
  • 1970-01-01
  • 2017-09-15
  • 1970-01-01
  • 1970-01-01
  • 2018-09-24
  • 1970-01-01
  • 2017-03-24
  • 1970-01-01
相关资源
最近更新 更多