This reason for using functions focuses on how code is contained in a named block such as main() to organize execution rather than placing instructions loose at the top level.
What is Encapsulation?
If you prompt a user with input() without doing this step, the provided data is lost and cannot be referenced in subsequent code.
What is assigning/storing the result in a variable?
int, float, str, and bool represent four fundamental examples of these in Python.
What are data types?
These three logical operators are used to construct complex Boolean expressions for range checking and multi-condition validity checks.
What is AND, OR, or NOT?
A local theater needs a function to handle ticket purchases. Write a Python program with a function named process_ticket that completes the following steps:
Parameters: Accepts a raw name string (name_input) and an age integer (age).
String Cleaning: Cleans name_input by removing leading/trailing whitespace and converting it to Capitalized Title Case (e.g., " jANE dOE " becomes "Jane Doe").
Conditionals & Logic:
If age is under 12 or 65 and older, assign a ticket price of $10.
Otherwise, assign a ticket price of $15.
Return Value: Returns a formatted string formatted as: "[Cleaned Name]: $[Price]".
Finally, write a main() function that prompts the user for their name and age using input(), performs any necessary type casting, calls process_ticket(), and prints the result.
def process_ticket(name_input, age):
clean_name = name_input.strip().title()
if age < 12 or age >= 65:
price = 10
else:
price = 15
return f"{clean_name}: ${price}"
def main():
raw_name = input("Enter your name: ")
raw_age = input("Enter your age: ")
age_int = int(raw_age)
result = process_ticket(raw_name, age_int)
print(result)
main()
The distinction between writing the code that defines a function and this action, which actually triggers the function to run.
What is calling (or invoking) a function?
Methods such as .strip(), .lower(), and .upper() are frequently used on user inputs to perform this essential task before processing.
What is defensive input cleaning?
This process converts a value from one data type to another, such as turning "42" into 42.
What is type casting?
Which conditional keyword would you use in a chained conditional program that has only 1 (a mutually exclusive) outcome?
What is elif?
These variables in a function definition allow it to accept dynamic inputs rather than using fixed or hardcoded values.
What are parameters?
Regardless of whether a user enters numbers or text, this is always the default data type returned by Python's built-in input() function.
What is a String (str)?
This specific error occurs if you attempt to cast a string like "hello" into an integer using int("hello").
What is a ValueError?
Which Boolean operator is NOT listed here:
==, >=, <=, >, <
What is != ?
This statement sends data from inside a function back to the caller; if omitted, the function implicitly outputs None.
What is a return statement?
The single equals sign (=) performs this operation, whereas the double equals sign (==) performs this operation.
What is assignment vs. equality comparison?
You must cast string inputs to this specific numeric data type if you plan to accept inputs containing decimal places.
What is float?
These conditional structures place at least 1 if block inside another if or elif block, expanding total outcome paths into a multi-tier branching logic tree.
What are nested conditionals?
This variable is created within a function and can only be accessed within that function, separating what happens inside a function from the calling scope.
What is local scope?
If a program executes
user_input = " alice_smith ".strip().title().replace("_", " ")
this is the final value and data type stored in user_input.
What is "Alice Smith"
A string that allows the programmer to display the value of variables within a string using { } and to perform in-line calculations.
What is an f-string/formatted string?
In nested conditionals, the initial outer condition acts as this, verifying general criteria before allowing execution to proceed to complex inner logic.
What is a "gatekeeper" conditional?