Week 3: Control Structures
Computing Programming with
Python
Ronny Estrella
● Conditionals
● Loops
CLASS
CONTENTS
Introduction
Class Computing Programming with Python
[email protected] )
Lecture Time Monday/Wednesday 10:30 am - 12:00 pm
Place Online via Zoom
Assignment 4 Week
Create a program that asks the user
for their name and age, then
calculates and displays their age in
dog years. The program should
include error handling and validation
for the age input.
Source: https://today.ucsd.edu/
Requirements Assignment
● The program should prompt the user for their name and age using the input()
function.
● The program should use an if statement to check if the age is valid (between 0 and
150 inclusive). If the age is invalid, the program should display an error message
and ask the user to enter their age again until a valid age is entered.
● The program should use a variable to store the user's age in human years and
another variable to store their age in dog years (calculated as age * 7).
● The program should output a message to the user that includes their name, age in
human years, and age in dog years.
Please enter your name: Alicia Wonder
Please enter your age: -5
Error: Age must be between 0 and 150.
Please enter your age: 25
Alice, you are 25 years old in human years and 175 years old in dog years.
Assignment 4 Week
Write a program that reads a list of
integers from the user and outputs the
following statistics:
● The minimum value
● The maximum value
● The range (maximum - minimum)
● The mean (average) value
● The median value (the middle value
when the list is sorted)
Source: https://statanalytica.com/
Requirements Assignment
● The program should prompt the user for a list of integers using the input() function.
The integers should be separated by commas.
● The program should convert the input string into a list of integers using the split()
and int() functions.
● The program should call a function name get_statistics() to calculate the statistics.
● The program should call a function name print_statistics() to output the statistics to
the user.
● The get_statistics() function should use built-in functions from the statistics
module in Python to calculate the mean and median values.
Please enter a list of integers (separated by commas): 5, 10, 15, 20, 25, 30, 35, 40, 45, 50
Statistics:
Minimum value: 5
Maximum value: 50
Range: 45
Mean (average) value: 27.5
Median value: 27.5
Review Quiz
● What is f-strings?
● What are the common data types?
● What is a expression?
● What is a variable?
● What are variable naming rules?
● Can i use “list” as variable name?
● What is IDLE?
Conditionals
● Allow the programmer to make decisions
● Program can have a choice between left or right upon certain conditions
● Within python there are a set of operators that can help us to ask
mathematical questions
● > and < symbols are probably quite familiar to you.
● >= denotes “greater than or equal to.”
● <= denotes “less than or equal to.”
● == denotes “equals”. A single equal sign would assign a value to a variable. Double
equal signs are used to compare variables.
● != denotes “not equal to.
if Statements
● If statements use bool or boolean values (true or
false)
● Then it will decide whether or not to execute a line
of code
Syntax:
if condition:
Statements
else:
statements
Control Flow, elif, and else
● Control flow allow us
to execute a line of
code in a specific order
● If we include elif the
programs make less
decisions
Time optimization is
always important
elif
● Multiple conditions can be chained with
elif(else if):
Syntax:
if condition:
statements
elif condition:
statements
else:
statements
What is the answer for the
Ultimate Question of Life,
the Universe, and
Everything ?
Let’s practice
● Checking if a number is positive or negative
● Checking if a user is an admin
● Checking if a password is strong enough
Practice Exercise:
How do you think you can check for special characters?
!@#$%^&*()-+?_=,<>/;
Or
● Allow a program to decide between one or more
alternatives
and
● Similar to or, but can be used within conditional
statements
For extra points
Implement a program that prompts the user for a greeting. If the greeting starts with “hello”, output $0. If the
greeting starts with an “h” (but not “hello”), output $20. Otherwise, output $100. Ignore any leading whitespace in
the user’s greeting, and treat the user’s greeting case-insensitively.
For the next class
● Write a program that asks the user to input their weight (in kilograms) and height (in
meters), and then calculates their BMI (Body Mass Index) using the following formula:
BMI = weight / (height ** 2). The program should then print a message indicating the
user's weight status based on their BMI. Here are the weight status categories:
○ BMI < 18.5: Underweight
○ 18.5 <= BMI < 25: Normal weight
○ 25 <= BMI < 30: Overweight
○ BMI >= 30: Obese
● The program should print "Your weight status is: [weight status category]" based on the
user's BMI.
TAKEAWAYS
● Conditions allow us to execute code in a
specific order
● Conditions allow us to do more complex
programming
● Complexity need to be decreased and the
Q&A
efficiency of our code need to be increased
Every question is welcome be
free to express your thoughts
Gracias!!!
Thank you
감사합니다