Programming ZyBooks2
Programming ZyBooks2
PARTICIPATION
ACTIVITY 2.1.2: If statement.
2.1 If-else statement
What is the final value of num_items?
1) bonus_val = 19
If statements num_items = 1
PARTICIPATION
ACTIVITY
2.1.1: If statement: Hotel discount. Check Show answer
2) bonus_val = 0
num_items = 1
hotel_rate = 155
if bonus_val > 10:
num_items = num_items + 3
hotel_rate = 155 user_age = int(input('Enter age: '))
If-else statement
An if-else statement executes one group of statements when an expression is true and another group
Python interpreter
Enter age: 68 of statements when the expression is false.
Your hotel rate: 135 135 hotel_rate
68 user_age Construct 2.1.1: If-else statement.
if expression:
# Statements that execute when expression is true (first
Animation captions: branch)
else:
# Statements that execute when expression is false (second
1. An if statement executes a group of statements if an expression is true. The program assigns branch)
©zyBooks 02/27/24 20:06 1927475 ©zyBooks 02/27/24 20:06 1927475
hotel_rate with 155 and then gets the user's age from input. Daniel Reyes Daniel Reyes
# Statements that execute after the branches
2. user_age is 68, so the expression 68 > 65 is true, and the if's statement will execute. Thus, the
CS119-2402A-06-2402A CS119-2402A-06-2402A
statement following the colon : will execute next.
3. hotel_rate is decreased by 20, which is the discount for guests older than 65.
4. The program completes by printing the hotel rate.
In the example below, if a user inputs an age less than 25, the statement
insurance_price = 4800 executes. Otherwise, insurance_price = 2200 executes.
Animation captions:
3) What is the final value of
1. An if-else statement executes a group of statements if an expression is true, and executes num_items?
another group of statements otherwise. bonus_val = 15
2. user_age is 22, so the expression 22 < 25 is true, and the if's statements will execute. num_items = 44
3. user's age is 40, so the expression 40 < 25 is false, and the else's statements will execute. if bonus_val < 12:
num_items = num_items + 3
else:
num_items = num_items + 6
num_items = num_items + 1
aside-elaboration
(Car insurance prices for drivers under 25 are higher because 1 in 6 such drivers are Check Show answer
involved in an accident each year, vs. 1 in 15 for older drivers. Source: www.census.gov,
2009)
©zyBooks 02/27/24 20:06 1927475 ©zyBooks 02/27/24 20:06 1927475
Daniel Reyes Daniel Reyes
CS119-2402A-06-2402A CS119-2402A-06-2402A
PARTICIPATION
ACTIVITY
2.1.4: If-else statements.
PARTICIPATION
ACTIVITY
2.1.5: Writing an if-else statement.
Translate each description to an if-else statement as directly as possible. (Not checked, but
Check Show answer
please indent a branch's statements some consistent number of spaces, such as 3 spaces.)
550678.3854950.qx3zqy7
Start
©zyBooks 02/27/24 20:06 1927475 ©zyBooks 02/27/24 20:06 1927475
Daniel Reyes Daniel Reyes
CS119-2402A-06-2402A Type the program'sCS119-2402A-06-2402A
output
num_apples = 5 CHALLENGE
2.1.3: Basic if-else.
if num_apples < 3: f ACTIVITY
print('a')
else:
k
print('f') Write an if-else statement for the following:
If user_tickets is less than 5, assign num_tickets with 1. Else, assign num_tickets with
print('k')
user_tickets.
1 2©zyBooks
02/27/24 20:06 1927475 ©zyBooks 02/27/24 20:06 1927475
Daniel Reyes Sample output with input: 3 Daniel Reyes
CS119-2402A-06-2402A CS119-2402A-06-2402A
Check Next Value of num_tickets: 1
CHALLENGE
ACTIVITY 2.1.2: Basic if-else expression.
Learn how our autograder works
550678.3854950.qx3zqy7
Write an expression that will cause the following code to print "18 or less" if the value of
user_age is 18 or less. Write only the expression. 1 user_tickets = int(input())
2
3 ''' Your solution goes here '''
Sample output with input: 17 4
5 print('Value of num_tickets:', num_tickets)
18 or less
550678.3854950.qx3zqy7
1 user_age = int(input())
2 if ''' Your solution goes here ''':
3 print('18 or less')
4 else:
5 print('Over 18')
Run
An if-else statement can be extended to have three (or more) branches. Each branch's expression is
View your last submission keyboard_arrow_down checked in sequence. As soon as one branch's expression is found to be true, that branch's statement
executes (and no subsequent branch is considered). If no expression is true, the else branch executes. PARTICIPATION
ACTIVITY 2.2.1: Multi-branch if-else statements.
Construct 2.2.1: Multi-branch if-else statement. Only 1 branch will execute. What is the final value of employee_bonus for each given value of num_sales?
if num_sales == 0:
employee_bonus = 0
if expression1: elif num_sales == 1:
# Statements that execute when expression1 is true employee_bonus = 2
# (first branch) ©zyBooks 02/27/24 20:06 1927475 elif num_sales == 2: ©zyBooks 02/27/24 20:06 1927475
elif expression2: Daniel Reyes employee_bonus = 5 Daniel Reyes
# Statements that execute when expression1 is false and expression2 is
CS119-2402A-06-2402A else: CS119-2402A-06-2402A
true employee_bonus = 10
# (second branch)
else:
# Statements that execute when expression1 is false and expression2 is 1) num_sales is 2
false
# (third branch)
The equality operator == evaluates to true if the left side and right side are equal. Ex: If num_years 2) num_sales is 0
holds the value 10, then the expression num_years == 10 evaluates to true.
less-than-or-equal-to operator (<=) rather than just the less-than operator. Thus, the constant is the
number of relevance, e.g., less-than-or-equal-to 39 rather than less-than 40, because 40 is not the 3) num_sales is 7
relevant number.
if num_years == 1: Enter number years married: 25 Write an if-else statement with multiple branches.
print('Your first year -- great!') Your silver anniversary --
elif num_years == 10: enjoy. If year is 2101 or later, print "Distant future" (without quotes). Otherwise, if year is 2001 or
print('A whole decade -- impressive.')
....
greater, print "21st century". Otherwise, if year is 1901 or greater, print "20th century". Else
elif num_years == 25:
print('Your silver anniversary -- enjoy.') (1900 or earlier), print "Long ago".
elif num_years == 50: ©zyBooks
Enter number 02/27/24
years 20:06
married: 30 1927475 ©zyBooks 02/27/24 20:06 1927475
print('Your golden anniversary -- amazing.') Nothing special. Daniel Reyes Daniel Reyes
else: CS119-2402A-06-2402A Sample output with input: 1776 CS119-2402A-06-2402A
print('Nothing special.') ....
©zyBooks 02/27/24 20:06 1927475 6 elif user_choice == 2: ©zyBooks 02/27/24 20:06 1927475
Daniel Reyes 7 if num_items < 0: Daniel Reyes
CS119-2402A-06-2402A 8 CS119-2402A-06-2402A
print('user_choice is 2 and num_items < 0')
9 else:
10 print('user_choice is 2 and num_items >= 0')
11 else:
12 print('user_choice is neither 1 or 2')
line that just executed
next line to execute
Run
A branch's statements can include any valid statements, including another if-else statement, which are
Determine the final value of sales_bonus given the initial values specified below.
known as nested if-else statements.
if sales_type == 2:
The below Python Tutor tool traces a Python program's execution. The Python Tutor tool is available at if sales_bonus < 5:
www.pythontutor.com. sales_bonus = 10
else:
sales_bonus = sales_bonus + 2
PARTICIPATION
ACTIVITY
2.3.1: Nested if-else else:
sales_bonus = sales_bonus + 1
©zyBooks 02/27/24 20:06 1927475 1) sales_type = 1; sales_bonus = 0; ©zyBooks 02/27/24 20:06 1927475
Daniel Reyes Daniel Reyes
CS119-2402A-06-2402A 0 CS119-2402A-06-2402A
1
10
2) sales_type = 2; sales_bonus = 4;
5 1 user_age = 26 # Hardcoded for this tool. Could replace with "int(input
2
6 3 # Note that more than one "if" statement can execute
10 4 if user_age < 16:
5 print('Enjoy your early years.')
3) sales_type = 2; sales_bonus = 7;
6
©zyBooks 02/27/24 20:06 1927475 ©zyBooks 02/27/24 20:06 1927475
8 Daniel Reyes 7 if user_age > 15: Daniel Reyes
CS119-2402A-06-2402A 8 print('You are old enough to drive.') CS119-2402A-06-2402A
9
9
10 10 if user_age > 17:
11 print('You are old enough to vote.')
Multiple distinct if statements 12
13 if user_age > 24:
Sometimes the programmer has multiple if statements in sequence, which looks similar to a multi- 14 print('Most car rental companies will rent to you.')
branch if-else statement but has a very different meaning. Each if statement is independent, and thus 15
more than one branch can execute, in contrast to the multi-branch if-else arrangement. 16 if user_age > 34:
17 print('You can run for president.')
PARTICIPATION line that just executed
ACTIVITY
2.3.3: Multiple distinct if statements.
next line to execute
PARTICIPATION
ACTIVITY
2.3.4: If statements.
©zyBooks 02/27/24 20:06 1927475 Determine the final value of num_boxes. ©zyBooks 02/27/24 20:06 1927475
Daniel Reyes Daniel Reyes
CS119-2402A-06-2402A CS119-2402A-06-2402A
1) num_boxes = 0
Check Next
num_apples = 9
CHALLENGE
ACTIVITY
2.3.1: Enter the output for the multiple if-else branches.
550678.3854950.qx3zqy7
Start
num_items = 6 Run
if num_items > 2:
b ©zyBooks 02/27/24 20:06 1927475 ©zyBooks 02/27/24 20:06 1927475
print('b')
Daniel Reyes
View your last submission keyboard_arrow_down Daniel Reyes
elif num_items < 9:
print('e')
r CS119-2402A-06-2402A CS119-2402A-06-2402A
else:
print('g')
print('r')
1 2
2.4 Equality and relational operators
Equality operators 4) y != 99
True
An equality operator checks whether two operands' values are the same (==) or different (!=). Note
that equality is ==, not just =. False
An expression involving an equality operator evaluates to a Boolean value. A Boolean is a type that has 5) x != y
just two values: True or False.
True
©zyBooks 02/27/24 20:06 1927475 ©zyBooks 02/27/24 20:06 1927475
Daniel Reyes False Daniel Reyes
Table 2.4.1: Equality operators. CS119-2402A-06-2402A CS119-2402A-06-2402A
PARTICIPATION
Equality Example (assume x is ACTIVITY
2.4.2: Creating expressions with equality operators.
Description
operators 3)
Type the operator to complete the desired expression.
x == 3 is True
== a == b means a is equal to b
x == 4 is False 1) num_dogs is 0
Indicate whether the expression evaluates to True or False. Check Show answer
x is 5, y is 7.
False
Check Show answer
2) x == y
True 4) num_dogs is either less than or
greater than num_cats
False ©zyBooks 02/27/24 20:06 1927475 ©zyBooks 02/27/24 20:06 1927475
Daniel Reyes num_dogs num_cats Daniel Reyes
3) y != 7 CS119-2402A-06-2402A CS119-2402A-06-2402A
False
x < 4 is True
< a < b means a is less than b PARTICIPATION
x < 3 is False ACTIVITY
2.4.4: Creating expressions with relational operators.
x > 2 is True
> a > b means a is greater than b Type the operator to complete the desired expression.
x > 3 is False
1) num_dogs is greater than 10
x <= 4 is True
<= a <= b means a is less than or equal to b x <= 3 is True num_dogs 10
x <= 2 is False
Check Show answer
x >= 2 is True
a >= b means a is greater than or equal
>= x >= 3 is True
to b 2) num_cars is greater than or equal
x >= 4 is False
to 5
num_cars 5
1 2
©zyBooks 02/27/24 20:06 1927475 ©zyBooks 02/27/24 20:06 1927475
zyDE 2.4.1: If-else with expression: Non-negative. Daniel Reyes Daniel Reyes
CS119-2402A-06-2402A Check Next CS119-2402A-06-2402A
The program prints "Zero" if the user enters 0, else prints "Non-zero".
Modify the program to print "Non-negative" if the user enters 0 or
greater, else print "Negative". Comparing characters, strings, and floating-point types
99 The relational and equality operators work for integer, character, and floating-point built-in types.
Load default template...
Floating-point types should not be compared using the equality operators, due to the imprecise
1 user_num = int(input('Enter a representation of floating-point numbers, as discussed in a later section.
2 Run
3 if user_num == 0: The operators can also be used for the string type. Strings are equal if they have the same number of
4 print('Zero') characters and corresponding characters are identical. If string my_str = 'Tuesday', then (my_str ==
5 else:
6 print('Non-zero') 'Tuesday') is True, while (my_str == 'tuesday') is False because T differs from t.
7
PARTICIPATION
ACTIVITY 2.4.5: Comparing various types.
Which comparisons will not result in a syntax error AND consistently yield expected results?
Variables have types denoted by their names.
1) my_int == 42
OK
Not OK
2) my_float == 3.14
CHALLENGE
ACTIVITY 2.4.1: Enter the output for the branches with relational and equality operators. OK
Not OK
550678.3854950.qx3zqy7
©zyBooks 02/27/24 20:06 1927475 3) my_string == 'Hello' ©zyBooks 02/27/24 20:06 1927475
Start
Daniel Reyes Daniel Reyes
CS119-2402A-06-2402A OK CS119-2402A-06-2402A
Type the program's output
Not OK
The types of the values being compared determines the meaning of a comparison. If both values are
numbers, then the numbers are compared arithmetically (5 < 2 is False). Comparisons that make no
sense, such as 1 < 'abc' result in a TypeError.
https://learn.zybooks.com/zybook/CS119-2402A-06-2402A/chapter/2/print 21/43 https://learn.zybooks.com/zybook/CS119-2402A-06-2402A/chapter/2/print 22/43
2/27/24, 8:06 PM zyBooks 2/27/24, 8:06 PM zyBooks
Comparison of values with the same type, like 5 < 2, or 'abc' >= 'ABCDEF', depend on the types PARTICIPATION
ACTIVITY 2.4.7: Watch out for assignment in an if-else expression.
being compared.
Numbers are arithmetically compared. What is the final value of num_items? Write "Error" if the code results in an error.
Strings are compared by converting each character to a number value (ASCII or Unicode), and
1) num_items = 3
then comparing each character in order. Most string comparisons use equality operators "==" or if num_items == 3:
num_items = num_items + 1
"!=", as in today == 'Friday'.
©zyBooks 02/27/24 20:06 1927475 ©zyBooks 02/27/24 20:06 1927475
Lists and tuples are compared via an ordered comparison of every element in the sequence.
Daniel Reyes Daniel Reyes
Every element between the sequences must compare as equal for CS119-2402A-06-2402A
an equality operator to CS119-2402A-06-2402A
evaluate to True. Relational operators like < or > can also be used: The result is determined by Check Show answer
the first mismatching elements in the sequences. For example, if x = [1, 5, 2] and y = [1, 4, 3], then
evaluating x < y first evaluates 1 < 1, then 5 < 4, which produces a value of False.
2) num_items = 3
Dictionaries are compared only with == and !=. To be equal, two dictionaries must have the same if num_items = 10:
set of keys and the same corresponding value for each key. num_items = num_items + 1
PARTICIPATION
ACTIVITY
2.4.6: Comparing various types.
Check Show answer
3) Click the expression that is True. 2.5 Boolean operators and expressions
{'Henrik': '$25'} == {'Daniel': '$25'}
(1,2,3) > (0,2,3)
Booleans and Boolean operators
[1, 2, 3] >= ['1', '2', '3']
A Boolean refers to a value that is either True or False. Note that True and False are keywords in
©zyBooks 02/27/24 20:06 1927475 Python and must be capitalized. A programmer can assign a Boolean value by
©zyBooks specifying
02/27/24 True
20:06 or
1927475
Common errors Daniel Reyes Daniel Reyes
False, or by evaluating an expression that yields a Boolean.
CS119-2402A-06-2402A CS119-2402A-06-2402A
A common error is to use = rather than == in an if-else expression, as in: if numDogs = 9:. In such
cases, the interpreter should generate a syntax error. Figure 2.5.1: Creating a Boolean.
Another common error is to use invalid character sequences like =>, !<, or <>, which are not valid
operators.
my_bool = True # Assigns my_bool with the boolean value True a and b Boolean AND: True when both operands are True.
is_small = my_val < 3 # Assigns is_small with the result of the expression a or b Boolean OR: True when at least one operand is True.
(False)
Boolean NOT (opposite): True when the single operand is False (and False
not a
when operand is True).
(age > 16) and (age < 25) True, because both operands are True.
a b a and b a b a or b a not a False, because both operands are not True (days >
(age > 16) and (days > 10)
False False False False False False False True 10 is False).
False True False False True True True False
True False False True False True True, because at least one operand is True (age >
True True True True True True (age > 16) or (days > 10)
16 is True).
Let x = 7, y = 9
not (days > 10) True, because operand is False.
(x > 0) and (y < 10) True (x < 0) or (y > 10) False not (x < 0) True
True True False False False not (age > 16) False, because operand is True.
(x > 0) and (y < 5) False (x < 0) or (y > 5) True not (x > 0) False
True False False True True not (user_char == 'q') False, because operand is True.
Animation captions:
PARTICIPATION
1. "and" evaluates to True only if BOTH operands are true. ACTIVITY 2.5.2: Evaluating expressions with Boolean operators.
2. "or" evaluates to True if ANY operand is True (one, the other, or both).
3. "not" evaluates to the opposite of the operand. Given num_people = 10, num_cars = 2, user_key = 'q'.
4. Each operand is commonly an expression itself. If x = 7, y = 9, then (x > 0) and (y < 10) is True
and True, so evaluates to true (both operands are True). ©zyBooks 02/27/24 20:06 1927475 1) num_people >= 10 ©zyBooks 02/27/24 20:06 1927475
Daniel Reyes Daniel Reyes
CS119-2402A-06-2402A True CS119-2402A-06-2402A
False
Table 2.5.1: Boolean operators.
Boolean
Description
operator
False
Check Show answer
5) not (user_key == 'a')
True 3) num_stores is between 10 and 20,
False inclusive.
if (num_stores >= 10) and (
6) user_key != 'a' ):
....
True
False
Check Show answer
7) not ((num_people >= 10) and
(num_cars > 2)) 4) num_dogs is 3 or more and
True num_cats is 3 or more.
if (num_dogs >= 3)
False :
8) (user_key == 'x') or ....
((num_people > 5) and
(num_cars > 1))
Check Show answer
True
False ©zyBooks 02/27/24 20:06 1927475 ©zyBooks 02/27/24 20:06 1927475
Daniel Reyes Daniel Reyes
CS119-2402A-06-2402A CS119-2402A-06-2402A
Boolean operators are commonly used in expressions found in if-else statements.
PARTICIPATION
ACTIVITY
2.5.3: Boolean operators: Complete the expressions for the given condition.
Fill in the missing Boolean operators. Assume all variables are integers.
1 special_num = int(input())
For most direct readability, your
2
expression should compare 3 if ''' Your solution goes here ''':
directly with the smallest and 4 print('Special number')
5 else:
largest 3-digit number.
6 print('Not special number')
if (num >= 100)
:
....
CHALLENGE
ACTIVITY 2.5.1: Enter the output of the Boolean expressions.
Run
550678.3854950.qx3zqy7
Check Next
Ineligible
1 user_age = int(input())
2
3 if ''' Your solution goes here ''': ©zyBooks 02/27/24 20:06 1927475 ©zyBooks 02/27/24 20:06 1927475
4 print('Eligible') Daniel Reyes Daniel Reyes
5 else: Run
CS119-2402A-06-2402A CS119-2402A-06-2402A
6 print('Ineligible')
View your last submission keyboard_arrow_down
Code blocks
Run
A code block is a series of statements that are grouped together. A code block in Python is defined by
its indentation level, i.e., the number of blank columns from the left edge. The initial code block is not
View your last submission keyboard_arrow_down indented. A new code block can follow a statement that ends with a colon, such as an "if" or "else". In
addition, a new code block must be more indented than the previous code block. The program below
CHALLENGE
2.5.4: Boolean operators: Branching using Boolean variables. includes comments indicating where each new code block begins.
ACTIVITY
The amount of indentation used to indicate a new code block can be arbitrary, as long as the
Write an expression that prints 'You must be rich!' if the variables young and famous are both programmer uses the same indentation consistently for each line in the block. Good practice is to use
True. the standard recommended 4 columns per indentation level.
A common error for new Python programmers is the mixing of tabs and spaces. Never mix tabs and
Sample output with inputs: 'True' 'True' spaces for indentation in the same program. Many editors consider a tab to be equivalent to either 3
or 4 spaces, while in Python a tab is equivalent only to another tab. A program that mixes tabs and
You must be rich!
space to indent code blocks will automatically generate an IndentationError from the interpreter in
Python 3. A good practice is to use spaces only when indenting code, and to set text editor options to
automatically use spaces when possible.
Learn how our autograder works
©zyBooks 02/27/24 20:06 1927475 ©zyBooks 02/27/24 20:06 1927475
Daniel Reyes Daniel Reyes
550678.3854950.qx3zqy7
CS119-2402A-06-2402A
Figure 2.6.1: Code blocks are indicated with indentation.CS119-2402A-06-2402A
1 young = (input() == 'True')
2 famous = (input() == 'True')
3
4 if ''' Your solution goes here ''':
5 print('You must be rich!')
6 else:
7 print('There is always the lottery....')
160000
Load default template...
# First code block has no indentation
zyDE 2.6.1: Code blocks and indentation. Figure 2.6.2: Some indentations are continuations of the previous line.
Fix the errors in the code below so that you can run the program.
Multiple lines enclosed within parentheses are implicitly joined into a single string (without
newlines between each line); use implicit line joining for very long strings or functions with
numerous arguments. Ex: All extra lines are indented to the same column as the opening
quotation mark on the first line.
When declaring list or dict literals, entries can be placed on separate lines for clarity.
declaration = ("When in the Course of human events, it becomes necessary for "
©zyBooks 02/27/24 20:06 1927475 ©zyBooks 02/27/24 20:06 1927475
"one people to dissolve the political bands which have connected "
Daniel Reyes "them with another, and to assume among the powers of theDaniel Reyes
earth...")
CS119-2402A-06-2402A CS119-2402A-06-2402A
result_of_power = math.pow(long_variable_name_left_operand,
long_variable_name_right_operand)
Figure 2.6.3: List, dict multi-line constructs. print('The temperature in New York is',
temperatures['New York'])
else:
Containers like lists and dicts can be broken into multiple lines, with the elements on print('The temperature in New York is unknown.')
separate, indented lines.
Sample output with input: 105
my_list = [ my_dict = {
1, 2, 3, 'entryA': 1, ©zyBooks 02/27/24 20:06 1927475 The city is melting! ©zyBooks 02/27/24 20:06 1927475
4, 5, 6 'entryB': 2
] }
Daniel Reyes Daniel Reyes
CS119-2402A-06-2402A CS119-2402A-06-2402A
550678.3854950.qx3zqy7
1 temperatures = {
PARTICIPATION
ACTIVITY 2.6.1: Indentation. 2 'Seattle': 56.5,
3 'New York': float(input()),
4 'Kansas City': 81.9,
1) The standard number of spaces to use 5 'Los Angeles': 76.5
6 }
for indentation is 4. 7
True 8 ''' Your solution goes here '''
9
False
1 # Get input from the user NOTE2: Make sure to copy and past your code from previous into here and update it to complete this
2 num1 = int(input()) lab
3 num2 = int(input())
4 Write a program whose inputs are three integers, and whose output is the smallest of the three values.
5 #Determine the smallest number
6 if num1 < num2: Ex: If the input is:
7 smallest = num1
8
9 else: 7
10 smallest = num2 15
11 3
12 #Print the smallest number
13 print(smallest)
the output is:
550678.3854950.qx3zqy7
Run your program as often as you'd like, before submitting
Develop mode Submit mode
for grading. Below, type any needed input values in the first LAB
ACTIVITY
2.8.1: LAB: Smallest of three numbers 10 / 10
box, then click Run program and observe the program's
output in the second box.
Enter program input (optional) main.py Load default template...
trending_flat trending_flat
©zyBooks 02/27/24 20:06 1927475
main.py ©zyBooks 02/27/24 20:06 1927475
Run program Input (from above) Daniel Reyes Outp 6 if (num1 < num2 and num1 < num3): Daniel Reyes
(Your program)
CS119-2402A-06-2402A 7 smallest = num1 CS119-2402A-06-2402A
8
Program output displayed here 9 elif (num2 < num1 and num2 < num3):
10 smallest = num2
11
12 else:
13 smallest = num3
14
15 #Print the smallest number
16 print(smallest)
https://learn.zybooks.com/zybook/CS119-2402A-06-2402A/chapter/2/print 37/43 https://learn.zybooks.com/zybook/CS119-2402A-06-2402A/chapter/2/print 38/43
2/27/24, 8:06 PM zyBooks 2/27/24, 8:06 PM zyBooks
p ( )
trending_flat trending_flat
main.py
Run program Input (from above)
(Your program)
Outp
Remember, you can always continue practicing to improve your score on the assignments.
If you scroll up, you can look for incomplete activities. A check mark will appear to the right of
each activity that has been completed. ©zyBooks 02/27/24 20:06 1927475 ©zyBooks 02/27/24 20:06 1927475
View your 'My Activity' tab to see the assignment completion from a birds Daniel
eye Reyes
view. (Get there by Daniel Reyes
CS119-2402A-06-2402A CS119-2402A-06-2402A
clicking on the course name at the top of this page --> My Activity on the bottom right)
To ensure your grades and scores sync with the VC gradebook, close out of this zyBook and click
on the next assignment link in Virtual Campus rather than clicking the next section in the zyBook
now.
2.10 LAB: Seasons
Write a program that takes a date as input and outputs the date's season. The input is a string to
represent the month and an int to represent the day.
©zyBooks 02/27/24 20:06 1927475 Run your program as often ©zyBooks 02/27/24
as you'd 20:06submitting
like, before 1927475
April Daniel Reyes Develop mode Submit mode Daniel Reyes
11 CS119-2402A-06-2402A for grading. Below, type any needed
CS119-2402A-06-2402Athe first
input values in
box, then click Run program and observe the program's
the output is: output in the second box.
Enter program input (optional)
Spring
If your code requires input values, provide them here.
In addition, check if the string and int are valid (an actual month and day).
trending_flat trending_flat
main.py
Blue Run program Input (from above)
(Your program)
Outp
65
Program output displayed here
the output is:
Invalid
LAB
2.10.1: LAB: Seasons 0 / 10
2.11 LAB: Leap year
ACTIVITY
1 input_month = input()
Daniel Reyes info This section has been set as optional by your instructor. Daniel Reyes
CS119-2402A-06-2402A CS119-2402A-06-2402A
2 input_day = int(input())
3
A year in the modern Gregorian Calendar consists of 365 days. In reality, the earth takes longer to
4 ''' Type your code here. '''
5 rotate around the sun. To account for the difference in time, every 4 years, a leap year takes place. A
leap year is when a year has 366 days: An extra day, February 29th. The requirements for a given year
to be a leap year are:
2) If the year is a century year (1700, 1800, etc.), the year must be evenly divisible by 400
Some example leap years are 1600, 1712, and 2016.
Write a program that takes in a year and determines whether that year is a leap year.
1913
LAB
ACTIVITY 2.11.1: LAB: Leap year 0 / 10
1 is_leap_year = False
2
3 input_year = int(input())
4
5 ''' Type your code here. '''
https://learn.zybooks.com/zybook/CS119-2402A-06-2402A/chapter/2/print 43/43