15. Practice Makes Perfect: Count - cannot print


#1

Here is the code that sort of works but the output seems to be incorrect.

I have tried everything for the code to print (e.g., before ‘return’, after ‘return’, different indentation levels, etc). While checking it, I had it print out what should have been input and I got something different out. I have reset the code and reset the page several times and I can’t seem to get it to work like the other pages (scrabble_score, anti_vowel, etc). Please help. Also, when I input this it was formatted but it seems the formatting is removed when I look at it online.

CODE:

seqnc = (raw_input("Input the sequence:"))
itm = (raw_input("Input the item to count: "))

def count(sequence,item):
    numtot = 0
    for it in sequence:
        if(item == it):
            numtot += 1
    return numtot
    
print (seqnc, itm) #used this to check if input was the same as output

INPUT
Input the sequence:adfadfa
Input the item to count: a

OUTPUT
(u’adfadfa ‘, u’a’)


#2

a function doesn’t execute unless called, so if you want to execute the count function you will need to call the function


#3

I have tried that without success. Maybe I’m not understanding what you mean though. The website says the code is good (green checkmark) and I’m allowed to move on but I cannot get it to print properly.

I am curious why my output adds letters that were not in the input.


#4

the aim was to write a function which count the number of items in sequence, it was never about getting proper about. The exercise assumes you know how to call your function if you want to get output for certain input

raw_input stores the input as unicode (so u of unicode) you can convert to string if you want to get rid of the u


#5

Yes, I understand the aim of the exercise. I was doing more than what was assigned. Thanks for letting me know about the unicode. That was helpful.


#6

have you figured out the function call yet?


#7

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