0

So I want to get the user to input a list of numbers (1 or 2 digits ex 2,34,4,35,36) and for python to send back how much total numbers there is but the problem is python counts each number as a unit when I want it to know the difference between 2 and 34 for example. Idk my brain isn't braining.

Initially , I tried the len function then I cam here where I saw the count function but even then it still doesn't work

i = input("enter a list")
count(i)

(count isnt even recognized? for some reason)

I decided to go back to len and I did this

i = input ("enter a list")
print ( len(i) )

It did work but it counts every single number separately it even counts the commas!!!

I put the list 34,34,34,35 and I got 11 and I wanted to get 4 because that how much numbers there is

Thank you for talking the time to read my ranting

JonSG
  • 10,542
  • 2
  • 25
  • 36
  • You need to [split](https://docs.python.org/3/library/stdtypes.html#str.split) the string on commas to get a list – G. Anderson May 10 '23 at 17:16

3 Answers3

1

Your best bet is to do spaces, instead of commas. This way, you can use the built in python .split() method which will split a string into a list.

For example, the following code has the following output. (Yours uses a input though, but since inputs are strings anyways, I just created a variable with a string since that is same variable type as inputs.

text = "34 35 36 37"
text = text.split()
print(text)
print(len(text))

That produces the output of:

['34', '35', '36', '37']
4
JayRL4
  • 26
  • 1
0

You could do i = (input("enter a list").count(",")) + 1

This counts the number of commas, and adds one since the last number will not have a comma.

Like the commenter said above, if you want a list of it separated by commas, you can do

i = input("enter a list").split(',')
print(len(i))
imad.nyc
  • 57
  • 10
0

The variable i is a string, therefore when you use len() it will count all the characters. To count the numbers separated by commas you can use the split function.

 i = input("enter a list")  #  34,34,34,35
 print(len(i.split(",")))
DasaniT
  • 148
  • 9