By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,660 Members | 1,105 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,660 IT Pros & Developers. It's quick & easy.

return length of string with given index????

P: 10
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........
Nov 18 '06 #1
Share this Question
Share on Google+
3 Replies


bartonc
Expert 5K+
P: 6,596
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

Expand|Select|Wrap|Line Numbers
  1. def StringListIndexer(strList, index): 
  2.     if index > len(strList):
  3.         return -1 # Return causes an immediate exit of the function
  4.     return len(strList[index])
  5.  
Then, in the same file you'll want some test code:
Expand|Select|Wrap|Line Numbers
  1.  
  2. aListOfStrs = ['hi', 'hello', 'goodbye']
  3. # remember that the first one is number zero
  4. print StringListIndexer(aListOfStrs, 1)
  5.  
Nov 18 '06 #2

P: 10
ah, ok seeing your example I understand it better.
Thank you...I really appreciate the help!
Nov 19 '06 #3

bartonc
Expert 5K+
P: 6,596
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.
Nov 19 '06 #4

Post your reply

Sign in to post your reply or Sign up for a free account.