[Tutor] Fwd: You know python?

Dennis Lee Bieber wlfraed at ix.netcom.com
Tue Feb 22 14:11:14 EST 2022


On Mon, 21 Feb 2022 15:04:54 -0600, Fatima Mehak
<bossladyofthefuture at gmail.com> declaimed the following:

>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.
>

	Please post with a client that doesn't trash indentation. Indentation
is critical in Python, and what got sent by your client is invalid.

>def format_address(address_string):
># Declare variables
>house_no = ""
>street_no = ""

	First: one does not "declare variables" in Python. You've created two
empty strings, and bound names to those strings (Python attaches names to
objects, names don't represent some address in memory into which objects
get placed). It is also common to use None rather than empty strings to
indicate something that doesn't (yet) have a value.

	Second: "street_no" is misleading if it is supposed to be a street NAME
(to many of us, street NUMBER and house number would be the same --
ignoring the confusion of things like "4 Mile Road").

># Separate the address string into parts
>sep_addr = address_string.split()
># Traverse through the address parts
>for addr in sep_addr:
># Determine if the address part is the
>if addr.isdigit():
>house_no = addr
>else:
>street_no = street_no+addr
>street_no = street_no + " "

	The above loop and conditional will take

		123 some 99 street

and first bind "house_no" to 123; second pass will add "some" to an empty
string and bind "street_no" to it; third pass will bind "house_no" to 99
completely losing the 123 value; fourth pass will add "street" to the
previous "some". The end result is

		99
		some street

	IS THAT WHAT IS DESIRED? Or is the "house number" presumed to be the
first item on the input IF and ONLY IF it is all numeric? If so, there is
no need for a loop: the first "word" either is or is not a house number,
and the street name is everything that is not a house number -- just
.join() with space separator.


># house number or part of the street name
># Does anything else need to be done
># before returning the result?

	It's your problem -- does it do what your description says it is
supposed to do?

># Return the formatted string
>return "house number {} on street named {}".format(house_no,street_no)
>print(format_address("123 Main Street"))
># Should print: "house number 123 on street named Main Street"
>print(format_address("1001 1st Ave"))
># Should print: "house number 1001 on street named 1st Ave"
>print(format_address("55 North Center Drive"))
># Should print "house number 55 on street named North Center Drive"


>>> parse_address("123 Main Street")
'House number 123 on street named Main Street'
>>> parse_address("1001 1st Ave")
'House number 1001 on street named 1st Ave'
>>> parse_address("55 North Center Drive")
'House number 55 on street named North Center Drive'
>>> parse_address("28th Street SE")
'House number None on street named 28th Street SE'
>>> parse_address("4 Mile Road")
'House number 4 on street named Mile Road'
>>> 

	Obviously my function has no way of knowing that the last one is
supposed to be a street called "4 Mile Road"

	There is NO LOOP in my function. There is a .split(), a .isdigit() test
(if True it binds house_number and removes the first word from the split),
and a .join() operation to combine the street name parts.

>_______________________________________________
>Tutor maillist  -  Tutor at python.org
>To unsubscribe or change subscription options:
>https://mail.python.org/mailman/listinfo/tutor


-- 
	Wulfraed                 Dennis Lee Bieber         AF6VN
	wlfraed at ix.netcom.com    http://wlfraed.microdiversity.freeddns.org/



More information about the Tutor mailing list