Count


#1

As the activity says:

The item you input may be an integer, string, float, or even another list!

So I assumed that if the item appears as a string, it should be counted as well.
Therefore in my if statement, i converted datatype to int and add it to count if it equals to item.
On console, it reflects the expected output but I received this error message:

Oops, try again. Your code looks a bit off--it threw a "invalid literal for int() with base 10: 'one'" error. Check the Hint if you need help!

what does this mean?


def count(sequence,item):
    #define variable to count number of times 'item' appears in 'sequence'
    count = 0
    for each in sequence:
        #convert each in sequence to int and compare to item
        if int(each) == item:
            #if each equals item then add to count by 1
            count += 1
    return count
    
print count([7,7,7,7,7,7,7,7],7) #8
print count([1.0,1,2,'1'],1) #3


#2

Yes It can be another list or even a string.
so suppose when CC tests it..

and it finds a list inside the input list..
and now youre trying to convert it to int..what it will give?

int([1,2,3,4]) --- > Error

or a string..

int('a') --- > Error

so using int() limits the usability of this function.


#5

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.