nil Becomes None
Lesson 7 of 48 in Coddy's Python for Ruby Developers course.
Python spells the absence singleton None and its booleans True and False. Ruby uses nil, true, and false. Test absence with value is None, corresponding to the intent of Ruby value.nil?. The Python test checks identity with the singleton, not whether a quantity is empty or zero. Ruby predicate names may end in a question mark; Python identifiers cannot use that suffix. Python prints booleans with capital letters and prints the word None for absence, whereas Ruby puts nil prints a blank line. Keep missing data distinct from legitimate zero values when adapting input records.
Ruby
value = nil
puts value.nil?
puts falsePython
value = None
print(value is None)
print(False)Porting trap
A Python condition such as if not seats catches zero as well as None. Use an explicit absence check when zero seats is valid data.
Challenge
BeginnerRead an integer seat count from 0 to 20. Print whether any seats remain, using Python's boolean spelling. On the next line print the count if it is positive, or None if it is zero. Initialize the reported value to None and replace it when seats remain.
Print only these lines, ending each with a newline. Keep the supplied input scaffold; it reads the stated values without prompts.
Your code goes in main.py. The locked driver.py starts your program and cannot be edited.
Try it yourself
# Starts the program in main.py. Edit main.py to solve the challenge.
from runpy import run_module
run_module("main", run_name="__main__")
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Python for Ruby Developers
1From Ruby to Python
Run Your First Python ProgramStrings and InterpolationNames and Mutable StringsDivision and ParsingFrom end to IndentationRecap: Rewrite a Receipt4Iteration and Transformations
each and Python foreach_pair and enumeratenext Becomes continueselect, map, and sort_byRecap: Build a Ranking2Values and Decisions
nil Becomes None== and equal?Zero Is False in Pythonelsif and Conditional ValuesRecap: Eligibility Rules3Collections and References
Array Methods and ListsParallel Assignment and TuplesHash Defaults and Dict Keysdup, Copies, and SetsRecap: Clean an Inventory6Reliable Python Programs
gets, input, and Formattingrescue and Context Managersrequire and Import GuardsHints Do Not Enforce TypesRecap: Validate a Batch9Project: Order Processing
Create the Order ModelAdd Pricing PoliciesPractice on your own: Online Python compiler