语法错误:解析时出现意外的EOF。我输错了吗?

时间:2016-09-12 04:52:00

标签: python

在尝试解决this计算机奥林匹克问题时,我遇到了这个错误:

Traceback (most recent call last):
File "haybales.py", line 5, in <module>
    s = input()
File "<string>", line 0

^
SyntaxError: unexpected EOF while parsing

我目前的代码如下:

hay = []
s = input()
while s != 'END':
    hay.append(s)
    s = input()

    for i in hay: #adds all integers in array
        var = var + i

       if var % hay[0] == 0:
            average = var / hay[0] #finds average of all numbers in array

            for bale in hay:
                diff = bale - average
                total += diff 
                #finds sum of all differences to find the final answer

             print 'you need to move' + str(total) + 'piles of hay'

        else:
            print 'it\'s not possible to make them equal'

我的输入是否被错误地读取? 如何更改代码以解决错误?

2 个答案:

答案 0 :(得分:1)

除了Andrew Hewitt建议的修补程序之外,您看到的错误是由于您在python2中使用input而不是raw_input(在python3 raw_input中)重命名为{{ 1}})。如果您检查input的{​​{3}},您会发现它需要有效的python代码,然后尝试运行它并返回其中的内容。 input只会将您的输入放在一个字符串中并将其返回。

所以只需使用raw_input并手动将您获得的字符串转换为所需的格式。

答案 1 :(得分:0)

您的代码存在许多问题:

  • <强>压痕: 在Python中缩进非常重要。您不能忽略它,否则您的代码将无法运行或产生意外结果。这可能是由于你将代码粘贴到堆栈溢出的方式,我承认这有时会很麻烦,但最好提一下以防万一。

    hay = []
    s = input()
    while s != 'END':
        hay.append(s)
        s = input()
    
    for i in hay: #adds all integers in array
        var = var + i
    
    if var % hay[0] == 0:
        average = var / hay[0] #finds average of all numbers in array
    
        for bale in hay:
            diff = bale - average
            total += diff
            #finds sum of all differences to find the final answer
    
        print 'you need to move' + str(total) + 'piles of hay'
    
    else:
        print "it's not possible to make them equal"
    
  • <强>输入: 您应该将输入转换为整数以允许calucation正常运行。理想情况下,您也会验证输入,但这是另一个主题。在这里,只需更改第4行:

    hay.append(int(s))
    
  • <强>范围: 变量的可见性在您的代码中有点偏差。例如,在第8行,您尝试向var添加值。但是每次运行循环时var都是一个新变量,会导致错误。 (var未定义)。这对Total来说也是一个问题。

    hay = []
    s = input()
    while s != 'END':
        hay.append(s)
        s = input()
    
    var = 0 ###ADD var HERE
    for i in hay: #adds all integers in array
        var = var + i
    
    if var % hay[0] == 0:
        average = var / hay[0] #finds average of all numbers in array
    
        total = 0 ###ADD total HERE
        for bale in hay:
            diff = bale - average
            total += diff
            #finds sum of all differences to find the final answer
    
        print 'you need to move' + str(total) + 'piles of hay'
    
    else:
        print "it's not possible to make them equal"
    
  • <强>计算: 平均值是所有值的总和除以值的数量。 hay [0]是第一个值,而不是值的数量。要获取输入的值的数量,可以使用len()函数。

    hay = []
    s = input()
    while s != 'END':
        hay.append(s)
        s = input()
    
    var = 0;
    for i in hay: #adds all integers in array
        var = var + i
    
    if var % hay[0] == 0:
        ###CHANGE BELOW LINE TO USE len(hay)
        average = var / len(hay) #finds average of all numbers in array
    
        total = 0
        for bale in hay:
            diff = bale - average
            total += diff
            #finds sum of all differences to find the final answer
    
        print 'you need to move' + str(total) + 'piles of hay'
    
    else:
        print "it's not possible to make them equal"
    
  • 打印: 打印时请记住在连接(连接)字符串之前和之后添加空格:

    print 'you need to move ' + str(total) + ' piles of hay'
    

否则输出时将没有空间。

我希望这会有所帮助。