8.13. ExercisesÂ¶
Write code that asks the user to enter a numeric score (0100). In response, it should print out the score and corresponding letter grade, according to the table below.
Score
Grade
>= 90
A
[8090)
B
[7080)
C
[6070)
D
< 60
F
The square and round brackets denote closed and open intervals. A closed interval includes the number, and open interval excludes it. So 79.99999 gets grade C , but 80 gets grade B.
A year is a leap year if it is divisible by 4; however, if the year can be evenly divided by 100, it is NOT a leap year, unless the year is also evenly divisible by 400 then it is a leap year. Write code that asks the user to input a year and output True if itâ€™s a leap year, or False otherwise. Use if statements.
Year
Leap?
1944
True
2011
False
1986
False
1800
False
1900
False
2000
True
2056
True
Above are some examples of what the output should be for various inputs.
What do these expressions evaluate to?
3 == 3
3 != 3
3 >= 4
not (3 < 4)
True
False
False
False
Give the logical opposites of these conditions, meaning an expression that would produce False whenever this expression produces True, and vice versa. You are not allowed to use the
not
operator.a > b
a >= b
a >= 18 and day == 3
a >= 18 or day != 3
Provided are the lengths of two sides of a rightangled triangle. Assign the length of the hypotenuse the the variable
hypo_len
. (Hint:x ** 0.5
will return the square root, or usesqrt
from the math module)Provided is a list of numbers. For each of the numbers in the list, determine whether they are even. If the number is even, add
True
to a new list calledis_even
. If the number is odd, then addFalse
.Provided is a list of numbers. For each of the numbers in the list, determine whether they are odd. If the number is odd, add
True
to a new list calledis_odd
. If the number is even, then addFalse
.Given the lengths of three sides of a triange, determine whether the triangle is right angled. If it is, the assign
True
to the variableis_rightangled
. If itâ€™s not, then assignFalse
to the variableis_rightangled
.Hint: floating point arithmetic is not always exactly accurate, so it is not safe to test floating point numbers for equality. If a good programmer wants to know whether
x
is equal or close enough toy
, they would probably code it up asif abs(x  y) < 0.001: # if x is approximately equal to y ...
Implement the calculator for the date of Easter.
The following algorithm computes the date for Easter Sunday for any year between 1900 to 2099.
Ask the user to enter a year. Compute the following:
a = year % 19
b = year % 4
c = year % 7
d = (19 * a + 24) % 30
e = (2 * b + 4 * c + 6 * d + 5) % 7
dateofeaster = 22 + d + e
Special note: The algorithm can give a date in April. You will know that the date is in April if the calculation gives you an answer greater than 31. (Youâ€™ll need to adjust) Also, if the year is one of four special years (1954, 1981, 2049, or 2076) then subtract 7 from the date.
Your program should print an error message if the user provides a date that is out of range.
Get the user to enter some text and print out True if itâ€™s a palindrome, False otherwise. (Hint: Start by reversing the input string, and then use the == operator to compare two values to see if they are the same)

: Write a program that will print out a greeting to each student in the list. This list should also keep track of how many students have been greeted and note that each time a new student has been greeted. When only one student has entered, the program should say "The first student has entered!". Afterwards, the program should say "There are {number here} students in the classroom!".students = ["Jay", "Stacy", "Iman", "Trisha", "Ahmed", "Daniel", "Shadae", "Tosin", "Charlotte"]  num_students = 0  for student in students:  print("Welcome to class, " + student) num_students += 1  if num_students == 1: print("The first student has entered!")  elif num_students > 1: print("There are " + str(num_students) + " students in the classroom!")

: Piece together a program so that it can successfully print out one print statement, given the value of x.x = 16  if x > 10:  if x > 20: print("This is a large number!")  else: print("This is a pretty big number.")
8.13.1. Contributed ExercisesÂ¶
condition1410: Write a program that will print out a greeting to each student in the list. This list should also keep track of how many students have been greeted and note that each time a new student has been greeted. When only one student has entered, the program should say "The first student has entered!". Afterwards, the program should say "There are {number here} students in the classroom!".students = ["Jay", "Stacy", "Iman", "Trisha", "Ahmed", "Daniel", "Shadae", "Tosin", "Charlotte"]

num_students = 0

for student in students:

print("Welcome to class, " + student)
num_students += 1

if num_students == 1:
print("The first student has entered!")

elif num_students > 1:
print("There are " + str(num_students) + " students in the classroom!")
You are given the following list of students taking a coding course: [‘Ruth’, ‘Karla’, ‘Mercedez’, ‘Mariam’, ‘Mahak’, ‘Luisa’, ‘Kimberly’, ‘Brytanya’, ‘Perla’, ‘Gabriella’, ‘Marissa’]. Do the following tasks.
 First print the following sentence in the console: The number of students in this course is 11. No hard coding! You need to use a Python function to get 11.
 Write a code that when a user inputs some name, it checks whether that name is contained in the list of students. If the student is present in the list it prints: Student _ is attending this course. Otherwise, if the name provided by the user is not present in the list, your program will print: Student _ is not attending the course.
 Test your code with two inputs: Irma and Karla (or some other student name taking the coding course). The first time your program should output: The number of students in this course is 11. Student Irma is not attending the course. For the second input it should output: The number of students in this course is 11. Student Karla is attending the course.
Given a list of desserts [‘ice cream’, ‘chocolate’, ‘apple crisp’, ‘cookies’] and your favorite dessert ‘apple crisp’ below, write a program using conditional statements that will for each dessert in list desserts capitalize its first letter and say if it is your favorite dessert or not. Hint: you need to use capitalize() method, for loops and use == to check whether a dessert equals your favorite dessert. Your code should print the following sentences:
Ice cream is not my favorite dessert.
Chocolate is not my favorite dessert.
Apple crisp is my favorite dessert.
Cookies is not my favorite dessert.
Q1: After completing the reading, what concepts are still unclear to you? If nothing is unclear, what did you find most interesting?
Ask the user to enter their favorite university, storing the result in the favorite_school variable.
If they enter WVU, West Virginia, or West Virginia University, print the text Let’s go Mountaineers!. Otherwise, print a message that they made a bad choice.
Prior to 2019, WVU only allowed students to D/F repeat a course if they had attempted less than 60 credit hours. Starting in 2019, WVU allows students to D/F repeat courses no matter how many credit hours they have attempted.
Write code that uses the current_year and credit_hours_completed variables to determine if the student is eligible to D/F repeat. Store the result as a True or False value in a variable named repeat_eligible.
The average monthly rainfall in inches recorded at the Van Nuys Airport for
2018 is stored in the rainfall
variable provided below.
Create a new list
called above_ave
and add a True
item
for each month that was above average rainfall and a False
item
for each month below average.
Ask users to input the width and length of a building in feet. Store the values in the width and height variables, respectively. Use conditional statements to determine which dimension is longer or if they are the same. Store the strings width, height, or same in the longer_dimension variable as appropriate.
Create a list called grades
that contains for the letter grade for each score in the list scores
according to the table
Score 
Grade 

>= 90 
A 
[8090) 
B 
[7080) 
C 
[6070) 
D 
< 60 
F 
The square and round brackets denote closed and open intervals. A closed interval includes the number, and open interval excludes it. So 79.99999 gets grade C , but 80 gets grade B.
Be sure to use an ifelifelse
pattern.
A year is a leap year if it is divisible by 4; however, if the year can be evenly divided by 100, it is NOT a leap year, unless the year is also evenly divisible by 400 then it is a leap year. Below are some examples of what the output should be for various inputs.
Year 
Leap? 

1944 
True 
2011 
False 
1986 
False 
1800 
False 
1900 
False 
2000 
True 
2056 
True 
Determine whether or not each year in the variable years
is a
leap year or not. If it is, add True
to a new list called
is_leap_year
, otherwise add False
.
A very simple way to tell if a number, \(N\), is prime is to try dividing \(N\) by all integers between 2 and \(N/2\). I.e.
Set
isprime=True
.Start with a test factor of 2.
Check if the number is divisible by the test factor.
If it is divisible, set
isprime=False
.For each odd number from 3 to \(N/2\), if \(N\) is divisible by the number, then set
isprime=False
Using this algorithm, determine whether or not 524287 is prime. Then
print
True
if it is prime andFalse
if it is not, andprint the number of iterations through the forloop you used to get the answer on a second line.
Provided is a list of numbers. For each of the numbers in the list, determine whether they are odd. If the number is odd, add True
to a new list called is_odd
. If the number is even, then add False
.
Provided is a list of numbers. For each of the numbers in the list,
determine whether they are even. If the number is even, add
True
to a new list called is_even
. If the number is odd,
then add False
.
Given the lengths of three sides of a triange, determine whether the triangle is right angled. If it is, the assign True
to the variable is_rightangled
. If it’s not, then assign False
to the variable is_rightangled
.
Hint: floating point arithmetic is not always exactly accurate,
so it is not safe to test floating point numbers for equality.
If a good programmer wants to know whether
x
is equal or close enough to y
, they would probably code it up as
if abs(x  y) < 0.001: # if x is approximately equal to y
...
You are given a variable number containing a positive integer greater than zero. Write code so that if number contains 1, 2, or 3, the spelled out words “one”, “two”, or “three”, respectively, are printed. If number is greater than 3, print the message “greater than three”.
Q1. Write a program to prompt the user to enter a test SCORE. Then, use if conditionals to print out the corresponding GRADE. That is SCORE >= 90; then print “Grade: A”, SCORE>=80; then print “Grade: B”, SCORE >= 70; then print “Grade: C”, SCORE>=60; then print “Grade: D”; otherwise print “Grade: F”.
Q2. Write a program to count how many letters “e” in the following given string. Store your count in a variable name e_count.
Use an if statement to print “big” if a value is greater than 100 and “small” if a value is less than 10 otherwise print “middle”. Check the value of a variable x
. Make sure your code gets pass for all three situations!