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

Hello,

why in the following code, line 2, I’m getting the ValueError from the title:

users_range = input("Vas range: ")
users_range = range(int(users_range[0]), int(users_range[1]))

Thank you.

Hi @ai-2090

Are you by any chance trying to do this:

Vas range: 2,3
Traceback (most recent call last):
  File "<pyshell#2>", line 1, in <module>
    users_range = range(int(users_range[0]), int(users_range[1]))
ValueError: invalid literal for int() with base 10: ','

If you are, as I suspect, then you’re getting that error because , cannot be converted into a base-10 integer. The return value from input() is always a string (str) type, so each index is an individual character and not a discrete entry like we’d get if we were working with a tuple or a list.

If you’re expecting the user to provide the bounds for your range, in the form of two comma-separated numbers, you’ll need to find a reliable way of determining where the comma is in the input string so you can convert… (or just prompt twice, for the lower and upper bounds). :slight_smile:

1 Like

Oh, now I understood sir, thank you very much.

Yeah, I was typing 1, 10, so besides the first index the other I wanted was actually the 4th, and not the 2nd (since the 3rd is a space). Nice thing to learn.

1 Like

If you type in 1, 10 and pull the 4th position (index 3), you’ll get 1 and not 10 so your code is still not doing what you want it to do.

Relying on a hard coded index here is never going to work for all use cases, which is the point I was trying to make before.

If you want the user to be able to provide the bounds for the range like this then you’re going to need to figure out some way to correctly, accurately and repeatably parse that input to get the two numbers for any use case.

What would happen if someone entered, for example, 1234,1345? Or 10, 1000? How about if I entered cheese, potatoes?

1 Like

Yeah you’re completely right and actually I did change the conception, I asked for 2 inputs by a user, for 1st and 2nd number in a range, that way I evade any indices (and also I put an if statement in case he’s not giving me numbers).

1 Like