Python 3文件输入语法错误

时间:2018-02-17 16:07:46

标签: python python-3.x

我将所有输入放入文件并进入终端以运行程序从文件获取输入但我得到语法错误。我哪里做错了?感谢。

Terminal Image

这是主程序。

# Create a list of 99 Boolean elements with value False
isCovered = 99 * [False]
endOfInput = False

while not endOfInput:
    # Read numbers as a string from the console
    s = input("Enter a line of numbers separated by spaces: ")
    items = s.split()  # Extract items from the string
    lst = [eval(x) for x in items]  # Convert items to numbers

    for number in lst:
        if number == 0:
            endOfInput = True
        else:
            # Mark its corresponding element covered
            isCovered[number - 1] = True

# Check whether all numbers (1 to 99) are covered
allCovered = True  # Assume all covered initially
for i in range(99):
    if not isCovered[i]:
        allCovered = False  # Find one number not covered
        break

# Display result
if allCovered:
    print("The tickets cover all numbers")
else:
    print("The tickets don't cover all numbers")

这是输入文件:

1 2 3 4 5 6 7 8 9 10
11 12 13 14 15 16 17 18 19 20
21 22 23 24 25 26 27 28 29 30
31 32 33 34 35 36 37 38 39 40
41 42 43 44 45 46 47 48 49 50
51 52 53 54 55 56 57 58 59 60
61 62 63 64 65 66 67 68 69 70
71 72 73 74 75 76 77 78 79 80
91 92 93 94 95 96 97 98 99 0

1 个答案:

答案 0 :(得分:0)

在python 3中,代码不显示错误。可能错误在于您使用两个版本中不同的输入命令(如果您使用python 2,请尝试使用raw_input()而不是input())

相关问题