return length of string with given index????

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • lostbuttrying
    New Member
    • Nov 2006
    • 10

    #1

    return length of string with given index????

    This is the exact question given...I wish I could start or make a stab, but I don't know where to begin:

    Submit a Python file named, 'countDemo.py', that defines and demonstrates a function that takes a list of strings and an index and returns the length of the string with the given index. If the index is not valid, the function should return a value of -1.

    Thank you in advance for any help or insight anyone can give........
  • bartonc
    Recognized Expert Expert
    • Sep 2006
    • 6478

    #2
    Originally posted by lostbuttrying
    This is the exact question given...I wish I could start or make a stab, but I don't know where to begin:

    Submit a Python file named, 'countDemo.py', that defines and demonstrates a function that takes a list of strings and an index and returns the length of the string with the given index. If the index is not valid, the function should return a value of -1.

    Thank you in advance for any help or insight anyone can give........
    In a file named 'countDemo.py', write something like

    Code:
    def StringListIndexer(strList, index): 
    	if index > len(strList):
    		return -1 # Return causes an immediate exit of the function
    	return len(strList[index])
    Then, in the same file you'll want some test code:
    Code:
     
    aListOfStrs = ['hi', 'hello', 'goodbye']
    # remember that the first one is number zero
    print StringListIndexer(aListOfStrs, 1)

    Comment

    • lostbuttrying
      New Member
      • Nov 2006
      • 10

      #3
      ah, ok seeing your example I understand it better.
      Thank you...I really appreciate the help!

      Comment

      • bartonc
        Recognized Expert Expert
        • Sep 2006
        • 6478

        #4
        Originally posted by lostbuttrying
        ah, ok seeing your example I understand it better.
        Thank you...I really appreciate the help!
        Your're welcome. I'm glad to help. Keep posting,
        Barton

        PS: Have you found the bug yet? There are two clues to where it is.

        Comment

        Working...