WHY IS IT SHOWING THIS ERROR

0 votes
n=int(input())

sum=0

for i in range(0,n):
     x=int(input())
     sum=sum+x
print(sum)

Traceback (most recent call last):  
File "Solution.py", line 4, in <module>   
x=int(input())
ValueError: invalid literal for int() with base 10: '1 2 3 4 10 11'
Jul 24, 2020 in Python by shrood
• 120 points

edited Jul 24, 2020 by MD 3,634 views

2 answers to this question.

0 votes

Hi@shrood,

If you are trying to sum the natural no., then you don't require to write the input function two times. Otherwise, it will ask you in every loop. You can use the below code.

n=int(input())
sum=0
for i in range(0,n):
     sum=sum+i
print(sum)
answered Jul 24, 2020 by MD
• 95,460 points
Hi!

not the natural numbers sir!

first input  "n"  represents the number of array  

and then taking n numbers as input in the for loop

and sum of all those numbers

Yes, I understood. But think once. According to your code, you are asking input inside the loop. So it will ask an input in every iteration. Try to analyze with a dry run. Take an example and try with pen and paper first.

oh ! understood

thanks a lot
0 votes

Hello @shrood ,

If you are trying to find the sum of numbers entered by the user then your code is correct.
The error is because the IDE you are using might be taking all the input first and then executed your sciprt. so first enter all the inputs required and then exectute your script.

For example:

5

4

1

3

2

5

15

Here n=5 and sum=15.

Tips: Try with diffrent IDE like pycharm(it will takes user input one at a time)


Thank you!!

answered Jul 24, 2020 by Niroj
• 82,840 points
yeah, it did

thanks a lot man!

Related Questions In Python

0 votes
1 answer

Python join: why is it string.join(list) instead of list.join(string)?

950down voteaccepted It's because any iterable can be ...READ MORE

answered May 15, 2018 in Python by aryya
• 7,460 points
913 views
0 votes
1 answer

Python join: why is it string.join(list) instead of list.join(string)?

This is because join is a "string" ...READ MORE

answered Jul 30, 2018 in Python by Priyaj
• 58,020 points
819 views
0 votes
1 answer

Why is Python scoping made the way it is?

The likeliest answer is that it just ...READ MORE

answered Nov 5, 2018 in Python by Nymeria
• 3,560 points

edited Dec 17, 2018 by Nymeria 676 views
0 votes
2 answers
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 7, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 4,434 views
0 votes
1 answer
+5 votes
6 answers

Lowercase in Python

You can simply the built-in function in ...READ MORE

answered Apr 11, 2018 in Python by hemant
• 5,790 points
4,070 views
0 votes
1 answer

when I run this, I get NameError: name 'state' is not defined error. How to solve this?

Hi@MUHAMMAD, I tried to run the exact code ...READ MORE

answered Sep 8, 2020 in Python by MD
• 95,460 points
2,829 views
0 votes
1 answer
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP