Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python

Travis Wion
Travis Wion
530 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
229,670 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))