Jump to content

Welcome to Geeks to Go - Register now for FREE

Need help with your computer or device? Want to learn new tech skills? You're in the right place!
Geeks to Go is a friendly community of tech experts who can solve any problem you have. Just create a free account and post your question. Our volunteers will reply quickly and guide you through the steps. Don't let tech troubles stop you. Join Geeks to Go now and get the support you need!

How it Works Create Account
Photo

Homework Help: counting function in Python


  • This topic is locked This topic is locked

#1
m0rik

m0rik

    New Member

  • Member
  • Pip
  • 1 posts
I already feel like fail for having to ask. I'm still browsing other forums as we speak.

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.

Assignment

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..*

Code

## 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."

Output

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.

  • 0

Advertisements


#2
sari

sari

    GeekU Admin

  • Community Leader
  • 21,806 posts
  • MVP
Sorry, we're not here to help with homework. If you're really stuck, then I suggest you ask your teacher. Topic closed.
  • 0






Similar Topics

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

As Featured On:

Microsoft Yahoo BBC MSN PC Magazine Washington Post HP