1 / 7
Jan 2022

Hello. At the beginning I will add that I am a beginner. I am doing a simple task of finding min and max values ​​from a set of numbers given by a user. I have trouble converting from input to int because it keeps crashing - "invalid literal for int () with base 10: …"
Below my code

lista=input()
lista=int(lista)
min=lista[0]
max=lista[0]
for i in lista:
if min>i:
min=i
if max<i:
max=i
print(min)
print(max)

I don’t know what I’m doing wrong with this conversion that I keep getting an error?

I think the problem is you’re trying to convert the whole list to an integer, rather than each element of the list.

Try something like this:

lista=input().split()
lista=[int(x) for x in lista]

(I’m no expert in Python, so there may be better ways of doing this.)

Thanks, it works. But there is also a problem that it only works when I enter numbers separated by a space into the list, and when I enter numbers separated by a comma, an error still pops up. How to make it count well after inserting numbers separated by a comma?

To be precise, I know that you can choose a separator in the split method, but I do not know how to protect yourself against different separators that the user can enter, such as space, comma?

You could give the user instructions.

Or read the numbers one at a time.

Or before splitting, you could replace any possible separators with your chosen one, then split on that. But beware of consecutive separators with nothing in between, such as a comma and a space between the numbers.

etc etc.

14 days later

Try something like this:

lista=input().split(",")

##the comma here acts as a delimiter and will split the list when you submit numbers separated by a comma between them.
But be careful if you have a space after the comma, you’ll need to mention it in the quotes.
Notice the space inside the quotes
For Example: ", "

Alternatively you may use: lista=[int(x) for x in lista.split(",")]