Truth Tests and Fallbacks
Lesson 9 of 48 in Coddy's Python for C# Developers course.
Use Python truth testing without losing valid zero values.
C# reference excerpt
Executable statements use C# top-level program style; type-only examples are declarations. System names assume the usual using System directive.
string name = "";
int? limit = 0;
Console.WriteLine(name.Length == 0 ? "Guest" : name);
Console.WriteLine(limit ?? 10);Python
name = ""
limit = 0
print(name or "Guest")
print(10 if limit is None else limit)What changes
C# ordinary conditions require bool; integers, strings and lists have no general automatic truth conversion. Python allows truth testing: None, False, numeric zero and empty built-in containers are false. Nonempty strings, including "False", are true. Python and/or short-circuit and return an operand, whereas ordinary C# &&/|| operate on booleans. User-defined C# true/false operators are a separate advanced feature, not arbitrary truthiness.
Transfer mistake
Python value or fallback is not C# value ?? fallback: or also replaces zero and empty values. An explicit None test preserves a real zero.
Your task
Read a display name and a limit token. Substitute Guest for an empty name. The scaffold converts the second line from missing or an integer in [0, 20] into None or int. Substitute 10 only for None, preserving zero. Output the selected name and limit on separate lines. Treat these as separate policies: empty-name fallback and nullable-limit fallback.
Challenge
BeginnerRead a display name and a limit token. Substitute Guest for an empty name. The scaffold converts the second line from missing or an integer in [0, 20] into None or int. Substitute 10 only for None, preserving zero. Output the selected name and limit on separate lines. Treat these as separate policies: empty-name fallback and nullable-limit fallback.
Editable implementation files: main.py. The locked driver.py is the supplied launcher or input/output harness and must remain unchanged.
Use the supplied input contract without prompts. Print only the required lines, each ending in a newline.
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 C# Developers
1C# Foundations in Python
Run the Python WorkspaceOutput, Strings, and CommentsTyped Locals to Python NamesDivision and RemaindersIndentation Instead of BracesRecap: Port a Receipt4Loops and Transformations
From foreach to forIndexes and Dictionary ItemsSentinels and Loop ControlFrom LINQ to ComprehensionsRecap: Stable Ranking2None and Decisions
Nullable Values and NoneEquality, Identity, and TypesTruth Tests and FallbacksBranch PriorityRecap: Eligibility Rules3Collections and References
From List<T> to listTuples and UnpackingDictionary Keys and ValuesSets, Aliases, and CopiesRecap: Stock Lookup6Reliable Python Programs
Input and Exact OutputExceptions and ValidationImports and the Main GuardType Hints and Runtime TypesRecap: Validate a Batch9Project: Order Processor
Project 1: Order ModelProject 2: Pricing PoliciesPractice on your own: Online Python compiler