can someone help me (again) stuck on ' hands on python'

Gary Wood pythonsky at sky.com
Thu Mar 19 10:40:55 EDT 2009


#I adjusted the 2nd  main function so i test what im trying to do can someone point me in the right direction please 

 
Exercise 2.3.3.1. ** Rename the example file locationsStub.py to be locations.py, and complete the function printLocations, to print the index of each location in the string s where target is located. For example, printLocations('This is a dish', 'is') would go through the string 'This is a dish' looking for the index of places where 'is' appears, and would return [2, 5, 11]. Similarly printLocations('This is a dish', 'h') would return [1, 13]. The program stub already uses the string method count. You will need to add code using the more general form of find. 


code 

'''Exercise to complete printLocations as described below.
Create file locations.py.'''
def printLocations(target):
    '''s is a string to search through, and target is the substring to look for.
    Print each index where the target starts.
    For example:
    >>> printLocations('Here, there, everywhere!', 'ere')
    1
    8
    20
    '''
  

def main():
    phrase = 'Here, there, everywhere!'
    print('Phrase:', phrase)
    for target in ['ere', 'er', 'e', 'zx']:
        print('finding:', target)
        printLocations(phrase, target)
    print('All done!')

main()


#i adjusted the main function helps me see the code as the program runs 
def main():
    phrase = 'Here, there, everywhere!'
    print('Phrase:', phrase)
    for target in ['ere', 'er', 'e', 'zx']:
        print('finding:', target)
        printLocations=(phrase, target)
        print(printLocations,'locations :',phrase.find(target))
    print('All done!')
   
main()

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20090319/b7e608c1/attachment.html>


More information about the Python-list mailing list