20230320computing Programming With Python (W3)
20230320computing Programming With Python (W3)
CLASS
CONTENTS
Introduction
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.
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
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
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
!@#$%^&*()-+?_=,<>/;
Or
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
Q&A
efficiency of our code need to be increased