Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python

error in list and tuples task

all I wanted to do here is to write a program that receives a sequence of digits separated by commas and generate its list tuple

a_list = []
n = int(input("Write the number of digits : "))

#adding elements to our list

for i in range(0,n): 
  digit = int(input())
  a_list.append(digit)

#converting our list to a tuple
a_tuple = (a_list)

print(a_list)
print(a_tuple)

I have got a : ValueError: invalid literal for int() with base 10: ''

What is wrong? How should I do it properly and what does the error mean? Thanks in advance

Steven Parker
Steven Parker
243,266 Points

When posting code, always use Markdown formatting, or make a snapshot of your workspace and post the link to that instead.

It's also very helpful to provide a link to the course page you are working with.

1 Answer

Steven Parker
Steven Parker
243,266 Points

It's difficult to understand the unformatted code, but it looks like there are a number of inputs being taken with no user prompts ("digit = int(input())").

Is it possible that for one of these something is being entered which is not a number?

all I wanted to do here is to write a program that receives a sequence of digits separated by commas and generate its list tuple

Steven Parker
Steven Parker
243,266 Points

Do you put each digit in one at a time? The error you are seeing indicates that the program is trying to convert something other than a digit into a number.

actually a sequence of digits like that: 3, 2, 4, 5 and so on... at once

Steven Parker
Steven Parker
243,266 Points

If you are entering commas (","), that might be the whole problem. A comma would be an "invalid literal" for conversion to a number.

Well someone just gave such a task haha Anyway, does that mean that I should input the digits, for example, one by one?

Steven Parker
Steven Parker
243,266 Points

That's what the program seems to expect now. Or you could re-write it to handle the other input (which I suspect was the intention of the assignment). Is this homework for school?

Yup - school homework :) This is why it motivated me to learn python haha