cheesedonuts
cheesedonuts

Reputation: 11

Syntax error: unexpected EOF while parsing. Am I inputting incorrectly?

While trying to solve this computer olympiad problem, I ran into this error:

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

^
SyntaxError: unexpected EOF while parsing

My current code looks like this:

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'

Are my inputs being read incorrectly? How can I change my code to resolve the error?

Upvotes: 0

Views: 298

Answers (2)

jadsq
jadsq

Reputation: 3382

In addition to the fixes suggeted by Andrew Hewitt, the error you see is caused by your use of input instead of raw_input in python2 (in python3 raw_input was renamed to input ). If you check the documentation for input you'll see that it expects valid python code then tries to run it and returns what ever comes out of it. raw_input will just put your input in a string and return it.

So just use raw_input and convert manually the string you get to the desired format.

Upvotes: 1

Andrew Hewitt
Andrew Hewitt

Reputation: 538

There are a number of problems with your code:

  • Indentation: In Python indentation is very important. You can't ignore it, or your code will either fail to run or give unexpected results. This may be due to the way you pasted the code into stack overflow, which I admit can sometimes be troublesome, but it's best to mention it just in case.

    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"
    
  • input: You should convert your input to an integer to allow the calucation to function correctly. Ideally you would also validate the input, but that's another topic. Here, just change line 4:

    hay.append(int(s))
    
  • Scope: Visibility of variables is a little off in your code. On line 8 for example, you attempt to add a value to var. But each time you run through the loop var is a new variable and will cause an error. (var is not defined). This is also a problem for 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"
    
  • Calculation: Average is the sum of all values divided by the number of values. hay[0] is the first value, not the number of values. To get the number of values entered you can use the len() function.

    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: When you print remember to add a space before and after your concatenated (joined) string:

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

Otherwise there will be no space when you get your output.

I hope this helped.

Upvotes: 0

Related Questions