This was multi-step process in which we had to isolate each name in a list (e.g. first, middle, last), select only our last name, then calculate the number of letters in it. This was achieved as follows:
1.
I used my full name “Philip Michael Coppola” as
the original string and split this string into a list, which I called “listName”
indexed as 0 (Philip), 1 (Michael), 2 (Coppola).
2.
I then used the script:
>>>lastNameLen = len(listName[-1])
>>>print lastNameLen
This
gave a result of ‘7’, the length of “Coppola”.
Due to the requirement that another person should be able to use this
code to print their last name, I had to use the [-1] index. This references the last item in a list,
whereas using [2] would only work if the other person put three names.
No comments:
Post a Comment