I just started taking Introduction to Computer Science course, and they are teaching Python as the language, using Python 2.5.4. Below are the details of the assignment, my code, and the output I receive.
fruit = "banana" count = 0 for char in fruit : if char == 'a' : count = count + 1 print count
As an exercise, encapsulate this code in a function named
countLetters(), and generalize it so that it accepts the string
and the letters as arguments. *Finished with great success!*
As a second exercise, rewrite this function so that, instead of
traversing the string, it uses the three-parameter version of
find() from the previous exercise. *Having trouble..*
## Find function meant to search through a string ## and find a character from a starting point (index) def find(str, ch, index) : ## len(str) - 1, because the length of the string needs ## to match the array of characters that make the string while index < len(str) - 1 : ## str[index-1], so user can input 1 for the first ## character position of a string if str[index-1] == ch : return index index = index + 1 ## Returns -1 to allow for a check later return -1 ## countLetters() function for previous exercise ## ## Function that counts the iterations of a user-defined ## ## character within a string ## def countLetters(str, char) : ## count = 0 ## for letter in word : ## if letter == char : ## count = count + 1 ## return count ## Function that counts the iterations of a user-defined ## character in a string def countLetters(str, char) : count = 0 ## Searches the string for a certain character and adds ## it to the count, reinitializing index to have a new ## starting point after the characters in the string ## it just finished searching while find(word, char, index) > 0 : count = count + 1 index = find(word, char, index) + 1 ## I think narrowed down the error to this statement return count ## Requests user input word = str(raw_input("Enter a word to examine: ")) char = str(raw_input("Enter a character to search for: ")) index = int(raw_input("Enter a character position from which to begin the search: ")) ## For grammatical correctness if countLetters(word, char) > 1 or countLetters(word, char) == 0 : print "There are " + str(countLetters(word, char)) + " \'" + char + "\'s in this word." else : print "There is " + str(countLetters(word, char)) + " \'" + char + "\' in this word."
Enter a word to examine: search this sentence
Enter a character to search for: s
Enter a character position from which to begin the search: 1
Traceback (most recent call last):
File "exercise.py", line 44, in <module>
if countLetters(word, char) > 1 or countLetters(word, char) == 0 :
File "exercise.py", line 33, in countLetters
while find(word, char, index) > 0 :
UnboundLocalError: local variable 'index' referenced before assignment
I've been struggling with the problem since yesterday, and I still haven't come up with a way to use the find() function to locate a certain character in the user-defined string and add it to a counter using the countLetters() function. I'm not looking for a step by step solution to the problem, but, if someone could point me in the right direction, I'd really appreciate it.
Edited by m0rik, 28 September 2010 - 09:15 PM.