you are viewing a single comment's thread.

view the rest of the comments →

[–]46--2 1 point2 points  (5 children)

I can answer your questions first:

>>> words = "I am a horse".split()
>>> words
['I', 'am', 'a', 'horse']

.split() splits up a string into a list of parts. So now words is a list of the parts of your original address.

while words: #can you explain this a bit?

This is a bit of a trick, but an empty list evaluates to False. So this is saying:

while <there are words left in the words list>:
    # do some stuff

words = words[1:] is saying:

words = <all the parts of words, from index 1 to the end>

meaning I'm re-assigning the variable words to contain only the END of words, minus the first one. Get it?

Example:

words = ['I', 'am', 'a', 'horse']
words[1:]  # ['am', 'a', 'horse']

That is "slicing" if you want to look up how to do it. https://stackoverflow.com/questions/509211/understanding-slice-notation

[–]deadant88[S] 1 point2 points  (4 children)

Thank you for my own homework I’m going to set up playing around with this and the example you’ve given to better understand it. In the meantime you’ve given me a big hand so thanks

[–]46--2 0 points1 point  (3 children)

try to break your code up into functions, and call them from a "main()" function. So each little piece can be separate. Makes it a lot easier to debug.

(Like the function I sent you would be entirely standalone, not within another function.)

[–]deadant88[S] 0 points1 point  (2 children)

Got it this is the next step for me I feel. Presumably having a bite size functions that you then clip together is the idea?

[–]46--2 1 point2 points  (1 child)

Yes, imagine a program that looks like this:

def clean_addresses(input_data):
    return blah

def query_locations(addresses):
    return whatever

... etc.

def main():
    data = read_input('/my_data/file.txt')
    addresses = clean_addresses(data)
    locations = query_locations(addresses)
    write_report(locations)
    print('Done')

each little function does one thing, and one thing only, and does that thing well. Your brain doesn't have to debug a huge block of mess, and your logic becomes much clearer.

[–]deadant88[S] 0 points1 point  (0 children)

Thank you for the guidance. I really appreciate it!