Darren Macis Darren Macis - 11 months ago 41
Python Question

Trying to better understand function instructions

I am having trouble understanding how to apply these two lines of instruction to this function, and was wondering if you could help by explaining it to me in layman's terms. I know how to split strings and convert int to string and reverse, as well as how to set variables and conditions, but I just can't seem to wrap my head around the i-th character bits.

# loop while i is less than the length of name and the i-th character is not a space.
# return the part of name up to but not including the i-th character to the caller.
def get_first_name(name):
i = 0
while i < len(name) and '' in str(i):
i += 1
return name

Answer Source

I am not implementing your function, but just explaining the logic as I believe this the part of your some exercise.

Your condition could be written as:

name = "Hello World"
i = 0

# len(name): will return the length of `name` string
# name[i] != " ": will check that item at `i`th position is not blank space

while i < len(name) and name[i] != " ":
    print name[:i+1]  # print string from start to the `i`th position
    i += 1

which will print:


Now, I guess you know how to put this logic in your function and which value to return ;)