
November 9th, 2012, 12:57 PM
|
|
Registered User
|
|
Join Date: Nov 2012
Posts: 1
Time spent in forums: 40 m 53 sec
Reputation Power: 0
|
|
Help with strings in lists
Hi everyone,
I am pretty new to Python and I am currently taking a course on intro to computer programming. We currently have an assignment on strings and lists. I'm currently out doing field work, so I haven't been able to talk to my professors and I figured I'd come here for some help!
So here is the problem, I am given a list of names
names = ['Bob', 'Susan', 'Mike']
With this list, I create a function that has two variables; one variable is the list, and the other variable is an integer. The integer represents the length of each string in the list. The return value is the number of names in the list that are greater than the integer. For example:
>>> my_function(list, integer)
>>> my_function(names, 4)
>>> 1 (this would be Susan)
So the code I wrote for this goes like this:
def my_function(list, length):
count = 0
for i in range(len(list)):
if len(list[i]) > length:
count = count + 1
return count
So this code seems to work properly, however, there is a second part to this problem that I need to solve.
When I return my number, the function must also update the original list to include empty spaces in the string equal to the length that I specified in my function. For example:
>>> names = ['Bob', 'Susan', 'Mike']
>>> my_function(list, integer)
>>> my_function(names, 4)
>>> 1
>>> names = ['Bob ', 'Susan', 'Mike']
or if
>>> names = ['Bob', 'Susan', 'Mike', '']
>>> my_function(list, integer)
>>> my_function(names, 10)
>>> 0
>>> names = ['Bob ', 'Susan ', 'Mike ', ' '] (for some reason the spaces are not showing up in the thread, each string now has a length of 10)
What I am having trouble with is writing the second part of the function that adds spaces to the end of each string in the list and then updates accordingly. Can anyone help me out with this? I have no idea where to even start with this.
Please remember that I am still really new to this!!! Thanks a lot!!
|