In this lab, you'll experiment with the floor division (//) and modulo (%) operators. You'll also practice writing conditional statements with if and if-else patterns.
Exercise 1: A school wants you to write a program that will help them divide students into classrooms. They want to have the number of students in each classroom be as equal as possible. Write a program that asks the user for the number of students and the number of classrooms, and then tells them how many students will be in each classroom.
Hint: Notice that there are four different numbers that appear in the output. I suggest having a separate variable that you calculate for each one of these. You will not need an if statement for this problem.
Recall that you can tell if a number is even or odd by checking its remainder when divided by 2, i.e., modulo 2. We could check this using the comparison operator == in the condition of an if-else statement.
Exercise 2: Type this code in a script in Thonny, and try several inputs to confirm that it works as you expect it to.
num = int(input("Enter your favorite integer: "))
if num % 2 == 0:
print(num,"is even")
else:
print(num,"is odd")
Exercise 3: Replace the == in the above code with = and run it again. Answer the following questions in your notes:
== and =?Exercise 4: The date July 3, 2021 is a magic date because, when written in this format: 7/3/21, the month times the day equals the two-digit year: $7 ∗ 3 = 21$. Write a program that asks the user to enter a month, a day, and a two-digit year and displays whether or not that date is magic. It should work like this:
month = int(input("Enter a number for a month (1-12): "))
day = int(input("Enter a day of the month (1-31): "))
year = int(input("Enter a two-digit year: "))
#write your `if-else` statement here
Exercise 5: You can include conditional statements inside of other conditional statements like in the example below. This is called a nested conditional statement because one of the conditional statements is nested inside the other - like a Russian nesting doll. Save this code as a script and run it with different inputs. In your notes, write a description of how the combination of conditional statements works. What would you do if you wanted it to be able to tell you whether or not a number is even and divisible by 3?
#this code checks if a number is divisible by both 2 and 3
if num % 2 == 0:
print(num,"is even")
else:
if num % 3 == 0:
print(num,"is odd and divisble by 3")
else:
print(num,"is odd and not divisible by 3")
Russian nesting dolls: https://commons.wikimedia.org/w/index.php?curid=13676809
Challenge Exercise 6: Write a program that will allow the user to enter a year, and then tells the user whether there are any presidential, guberntorial (i.e., governor), US Senate, or US House elections in Iowa that year. Note the following:
Write your program so that the output has the following format:
Hints:
% operator with the years. Try practicing with each of the sample years and the % operator in the interactive shell, and see if you can come up with a rule that works for the list of years given for each office.if-else statement - that is, I'd expect a sequence of at least 4 different if-else statements.When you are finished, submit your solution to the Lab 5: Election years assignment on codePost. This problem will be auto-graded, and it will be checking that your output matches the sample above exactly. So, if you think your solution is correct, but codePost doesn't give you the points, check the messages it is giving you - there may be a difference in spelling, punctuation, etc.