unexpected EOF while parsing error in this code

This is the place for queries that don't fit in any of the other categories.

unexpected EOF while parsing error in this code

Postby KillerCode » Sat Oct 26, 2013 2:25 pm

Here is my code.I want to get the sum of the absolute values.But instead of that I get this error.

Code: Select all
    no=int(input())
    list=input()
    words=list.split(" ")
    total=0
    for x in words:
        total=total+int(str(abs(x))
   


example inputs:

6
7 -8 6 9 -3 4

answer should be 37.

So how can I do this?

Thank you very much.
KillerCode
 
Posts: 3
Joined: Sat Oct 26, 2013 9:32 am

Re: unexpected EOF while parsing error in this code

Postby Mekire » Sat Oct 26, 2013 2:34 pm

Code: Select all
user_input = input("Enter a list of numbers seperated by spaces: ")
sequence = map(int,user_input.split())
sum_of_abs = sum(abs(number) for number in sequence)
print(sum_of_abs)

-Mek

Edit:
Basically this would have fixed your original code:
Code: Select all
no=int(input())
list=input()
words=list.split(" ")
total=0
for x in words:
    total=total+abs(int(x))
Also you shouldn't use list as a variable name; it is a python builtin that you don't want to overwrite.

Edit2:
Oh... the EOF... you are using Python 2 aren't you. You need to use raw_input instead of input if this is the case.
User avatar
Mekire
 
Posts: 988
Joined: Thu Feb 07, 2013 11:33 pm
Location: Amakusa, Japan


Return to General Coding Help

Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 4 guests