1

How do i add all imputed numbers in a string?

Ex:

input:
5 5 3 5
output
18

and it must supports ('-')
Ex.

input
-5 5 3 5
output
8

I write something like this:

x = raw_input()
print sum(map(int,str(x)))

and it adds normally if x>0
But what to do with ('-') ?
I understand that i need to use split() but my knowledge is not enough (

0

3 Answers 3

2

You're close, you just need to split the string on spaces. Splitting will produce the list of strings ['-5', '5', '3', '5']. Then you can do the rest of the map and sum as you intended.

>>> s = '-5 5 3 5'
>>> sum(map(int, s.split()))
8
Sign up to request clarification or add additional context in comments.

Comments

0

its simple

>>> input = raw_input('Enter your input: ')
    Enter your input: 5 5 10 -10
>>> list_numbers = [int(item) for item in input.split(' ')]
>>> print list_numbers
    [5, 5, 10, -10]

And after what you want :)

Comments

0

You can use the following line: sum(map(int, raw_input().split()))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.