0

When I run my code in python IDLE it works. But when I run my code on HackerRank I get a ValueError. What can I do?

list=[]
n=int(input())
sum=0
for i in range(0,n):
    app=int(input())
    list.append(app)
for j in list:   
    sum=sum+j
print(sum)
Traceback (most recent call last):
  File "Solution.py", line 5, in <module>
    app=int(input())
ValueError: invalid literal for int() with base 10: '1 2 3 4 10 11'
Gino Mempin
  • 25,369
  • 29
  • 96
  • 135
Sathish
  • 1
  • 2
  • 3
    In Python `list` is a type. If you use it for a name you can confuse things. – Stephen Rauch Jun 30 '19 at 04:28
  • As said by @StephenRauch, you cannot use int() to covert a `list`. Besides, online platforms have a lot of problems with STDIN and STDOUT formats. It might be that that you're using the wrong type for a correct input. – shiv_90 Jun 30 '19 at 04:36
  • The Python `input()` function inputs an entire line of text. So if you want to input 6 numbers, you have to enter them one per line. If you enter them all on a single line, then you end up with a single space-separated string, as you saw. So of course you get an error when you pass it to `int`. – Tom Karzes Jun 30 '19 at 04:40
  • This code works if `input()` returns a string containing a single integer such as `'5'`, but not when it contains many integers such as in your example. – John Gordon Jun 30 '19 at 04:40
  • Check the requirements of that HackerRank problem. If I remember correctly, some of them will pass a list of numbers as input, not 1 number at a time. If you try passing in "1 2 3 4 10 11" as input on IDLE, you will get the same problem. – Gino Mempin Jun 30 '19 at 04:43

1 Answers1

0

In Hackerrank generally your list will be a space generated value so for that you can use this snippet, you can go through map concept in python "http://book.pythontips.com/en/latest/map_filter.html"

n = int(input())

li = map(int, input().split())

sum = 0

for i in li:
   sum += i
print (sum)