(ValueError: invalid literal for int() with base 10)

43 Views Asked by At
total = {}
count = 0
while(count < 4):
    _input = input()
    count = count + 1
    total[_input] = 0
    for i in range(5):
        _input2 = int(input())
        total[_input] = total[_input] + _input2      
print(total)
Virat,101,88,93,0,120
Rohit,50,100,30,140,80
Sky,10,20,30,40,50
Shreyas,100,0,0,100,0

Expected output:

{'Virat': 402, 'Rohit': 400, 'Sky': 150, 'Shreyas': 200}

The code works perfectly fine on my system, but it shows error

ValueError: invalid literal for int() with base 10

where I am solving questions

1

There are 1 best solutions below

0
Vinay Davda On

I checked your code running fine if you give input one by one. Like,

Virat
101
88
93
0
120
Rohit
50
..

If you want to give input like Virat,101,88,93,0,120, It stores value like {'Virat,10,20,30,40,50': 0} And second input is '' and when it does int('') it will throw: ValueError: invalid literal for int() with base 10: ''

But if you want to give input like Virat,101,88,93,0,120, below is modified code:

total = {}
count = 0
while(count < 4):
    _input = input()
    count = count + 1
    name = _input.split(',')[0]
    runs = _input.split(',')[1:]
    total[name] = 0
    for run in runs:
        total[name] = total[name] + int(run)
print(total)

Where you can give input same as you displayed:

Virat,101,88,93,0,120
Rohit,50,100,30,140,80
Sky,10,20,30,40,50
Shreyas,100,0,0,100,0

And get output like: {'Virat': 402, 'Rohit': 400, 'Sky': 150, 'Shreyas': 200}