1

I'd like to accept a user input consisting of several comma-separated integers and put them in a list. The values are separated by commas. Let's say I'm giving 100,97,84 as input, the desired output would be [100, 97, 84]. I'm trying the following code:

int(input("Please enter the numbers: "))

but it gives me:

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

What should I do?

Georgy
  • 12,464
  • 7
  • 65
  • 73
Jony Hadary
  • 17
  • 1
  • 2

3 Answers3

1

input will always return a string, even if it is a digit

If you want a number you write int(input"Enter number :))

I don't know what's the relation between your output and input, however you are looking to enter a list as an input. You can do that by

s = (input("Enter the numbers :"))
numbers = list(map(int, s.split()))

This will return a list in numbers. You enter the numbers with a space in between not a comma

E.g

Enter the numbers :100 97 84
Ouput>>>[100, 97, 84]

If you want them without the brackets and separated by a comma, you can use print(*numbers, sep=',') will give

Enter the numbers :100 97 84
Output>>>100,97,84
Abhishek Rai
  • 2,159
  • 3
  • 18
  • 38
0

If you want to write number separated by a comma, first you have to split your input into a list of digit only strings and then map this list through int:

numbers = [int(s) for s in input("Please enter numbers separated by a comma:").split(',')]
Marcin
  • 1,321
  • 7
  • 15
0

Because your phrase contains a comma, put it in a quotation mark to convert it to an integer with the following phrase :

s = (input("Enter the numbers :"))
result = ''.join([i for i in s if  i.isdigit()])
print(result)

example

Enter the numbers :12,34

output:

1234

The error problem will be solved

Salio
  • 1,058
  • 10
  • 21
  • 2
    No, the OP wants to enter three distinct integers, not one big integer. – smci Nov 01 '20 at 17:08
  • I think the numbers are entered in the form of 123,456, which is a combination of numbers and commas that are converted to numbers with the above code. – Salio Nov 01 '20 at 17:14