Python

Travis Wion
Travis Wion
491 Points

how can i get the count function to distinguish the differences between the numbers 1 and 10

one = input("What is your number: ") print(one.count("1" ,0, 100)) print(one.count("2",0 , 100)) print(one.count("3",0 , 100))

1 Answer

Steven Parker
Steven Parker
155,601 Points

I'm assuming you're inputting a list of numbers (or why else use "count"?) and separating them with whitespace.

The issue here is that you are using the string version of "count", which is only looking at the individual digits. To distinguish multi-digit numbers, you could convert the input into a list, and use the list version of "count". Here's an example:

nums = input("What are your numbers: ").split()
print("ones:  ", nums.count("1"))
print("twos:  ", nums.count("2"))
print("threes:", nums.count("3"))

A bit more sophisticated approach would be to convert the strings into integers, and then do numeric comparisons:

nums = [ int(n) for n in input("What are your numbers: ").split() ]
print("ones:  ", nums.count(1))
print("twos:  ", nums.count(2))
print("threes:", nums.count(3))