[Tutor] Fwd: You know python?

Alan Gauld alan.gauld at yahoo.co.uk
Tue Feb 22 14:32:46 EST 2022


On 21/02/2022 21:04, Fatima Mehak wrote:
> Let me know if you know how to fix the code below to include house numbers
> without generating a whole new code altogether. When I got it done it
> didn't include the house numbers but included the street names only. I hope
> I'm making coding sense here. Thanks.

Your code lost all indentation, be sure to post in plain text.
I'll try to guess what it might look like and remove some
of the irrelevant bits...

> def format_address(address_string):
>    sep_addr = address_string.split()
>    for addr in sep_addr:
>        if addr.isdigit():
>           house_no = addr
>        else:
>           street_name += addr
>           street_name += " "
            # Or replace above lines with:
            # street_name = "{} {}".format(streetname, addr)
> >    return "house number {} on street named {}".format(house_no,street_no)

Perhaps a better way still is to only use the first field for the house
Then use join() to put the subsequent fields in a string. Thus replace
the whole for loop with:

if sep_addr[0].isdigit(): house_no = sep_address[0]
else: house_no = ""
street_name = " ".join(sep.addr[1:])

I hope I guessed right and I hope my suggestions make sense.

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos




More information about the Tutor mailing list