[Tutor] getting unknown results
Alan Gauld
alan.gauld at yahoo.co.uk
Fri Apr 24 07:48:40 EDT 2020
On 24/04/2020 08:52, shubham sinha wrote:
> my code:
> def format_address(address_string):
> # Declare variables
> number = []
> place = []
> # Separate the address string into parts
> split_string = address_string.split()
> # Traverse through the address parts
> for i in range(0, len(split_string)):
> # Determine if the address part is the
> # house number or part of the street name
> if split_string[i].isnumeric():
> # Does anything else need to be done
> # before returning the result?
> number.append(split_string[i])
> return place.append(split_string[i])
> # Return the formatted string
> place = " ".join(place)
> return "house number {number} on street named {place}".format(number
> ,place = " ".join(place) )
This code should give you an error so presumably it is not the code
you ran. That makes it difficult to give sensible critique.
In particular the string formatting at the end is all wrong. I suspect
you wanted something like:
return "house number {} on street named {}".format(number,place)
You had already done the join() on the previous line...
That won't give you exactly what you want but it at least
prints without errors!
Dave has already given you pointers to what else is going wrong.
I'd just add that you are printing the result of your function.
That means you must look at what your function returns and when.
There are two return statements in your code. Which one gets executed
first? What does it return?
--
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