[Tutor] getting unknown results
DL Neil
PyTutor at danceswithmice.info
Fri Apr 24 06:10:12 EDT 2020
On 24/04/20 7:52 PM, shubham sinha wrote:
> The format_address function separates out parts of the address string into
> new strings: house_number and street_name, and returns: "house number X on
> street named Y".
> The format of the input string is: numeric house number, followed by the
> street name which may contain numbers, but never by themselves, and could
> be several words long.
> For example, "123 Main Street", "1001 1st Ave", or "55 North Center Drive".
>
> 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) )
>
> 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"
>
>
> my output:
>
> None
> None
> None
>
>
> I am getting result "none" instead of expected result.
>
> Please help me through this.
Well done! You seem to have the necessary ingredients. The challenge is
to look at the detail of Python...
Firstly, a really good way to learn and experiment with Python features
which are new-learning is the REPL. Fire-up Python from the
command-line, and then you can type-in individual statements and have
them resolve immediately.
For example, to verify that code does what you expect, instead of:
split_string = address_string.split()
use some sample-data and type the following directly into 'Python':
"123 Main Street".split()
Is the result exactly as you expected? (hope-so, but if not, you've
saved yourself some time and frustration!)
So, that's a program-design technique. How about debugging at
execution-time?
May I suggest copy-pasting this code into a tool such as
http://www.pythontutor.com/ which will illustrate how the computer
executes code line-by-line? (as do some editor/IDE packages)
Rightly, or wrongly, the impression gained from this code is that a
couple of functions work differently from the way you seem to expect.
One particular problem is that every time the code finds a numeric value
within the input, the code-line:
return place.append(split_string[i])
is returning None (as you noted). Why is the answer being appended to a
list? Should there be two separate statements, if the list is to be
returned (ie after being appended)?
- both of the above techniques will illustrate such matters.
You seem to have used other programming languages. Python has a much
(much, much...) easier way of handling loops by completely dropping
'pointers'. Instead of:
> 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():
try:
for token in address_string.split():
if token.isnumeric():
...
What happens here is that the address is split into "tokens" and the
for-loop enables consideration of each token in-turn. So much easier!
For toy-problems like this and whilst you are learning, it's important
to realise that we cannot 'see' what the computer is doing (apart from
the illustrative-tool(s) mentioned earlier). Another common technique is
to add 'debug print' statements to show what is happening, eg what
exactly is the value called "token" 'this time' through the loop? From:
for token in address_string.split():
if token.isnumeric():
try:
for token in address_string.split():
print( token, token.isnumeric() ) # this is it!
if token.isnumeric():
Thus the print() provides a 'window' into the computer, showing you at
every cycle of the loop, the token (string) it is considering and
whether/not that is a numeric value (boolean)!
They are called "debug print" because they are only for fault-finding
and you will take them out before handing-in the assignment (or putting
code into 'production')!
NB there are other ways of doing this, but debug-print is the easiest
for beginners to learn/add to practical exercises.
Enough?
Why don't you try that much, and see how things improve - then revert if
other questions arise...
--
Regards =dn
More information about the Tutor
mailing list