Jump to content


Photo

Chapter6, Page 177


  • Please log in to reply
1 reply to this topic

#1 Jon_Bryce

Jon_Bryce

    New Member

  • Members
  • Pip
  • 5 posts

Posted 22 August 2013 - 09:03 PM

Hi all;

Been working my way through this (always wanted to learn Python, and now I have some time). Something in the code on page 177 is interesting.

The "get_coach_data" returns "None" when there's not a file there - but the main body of the code continues on after display the 'File error..' message as it tries to 'pop' the data off the list that wasn't returned.

I put in some code to get around this - by trial and error I found that if I checked if the str(sarah) was 'None' then skip the 'pop' and the 'print'. I tried some snappy code to see if I could find out what 'None' was - I looked at type(sarah) (NoneType) and also checked to see if the len(Sarah) was zero. The best way I saw of doing it was to string(Sarah) and see if it was text 'None'.

There's got to be a better way... I'll read on... but, what's the better way (go ahead .. tell me how it comes out).

#2 paulbarry

paulbarry

    Advanced Member

  • O'Reilly Author
  • PipPipPipPipPipPipPipPip
  • 307 posts

Posted 23 August 2013 - 02:37 AM

You can use a check like this:

    if sarah is None:
         # do something
    else:
         # do something else

The value of None in Python is known as null, nil or undefined in other programming languages. It's basically a "nothing" value.

The code in this example could be improved (as you have deduced). The get_coach_data() function can return one of two values, and the calling code really should check for this (but doesn't in this example).

--Paul.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users