Lab 5: Division, Modulus, and Conditionals¶

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:

  • What kind of error does this give you? Why do you think Python gives that error?
  • What is the difference between == 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:

</p> Here's some code to get you started:
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")

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:

  • Presidential years happen every four years, in years like 2020, 2024, 2028, etc.
  • Iowa Gubernatorial elections happen every four years, in years like 2018, 2022, 2026, etc.
  • Iowa has two US Senate seats, and they each go up for election every six years. However, they are staggered. One of the seats goes up for election in years like 2016, 2022, 2028, etc. The other goes up for election in years like 2014, 2020, 2026, etc.
  • US House elections happen in every even year.
  • Sometimes, there are special elections that happen outside the normally scheduled times to replace an official who has resigned or died in office. You can ignore these cases.

Write your program so that the output has the following format:

Hints:

  • You will need to use the % 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.
  • Take each of the offices one at a time. First, try to get it to correctly say when it is a presidential year, then move on to each of the House, Gubernatorial, and Senate elections one at a time. Each of these will probably be a separate if-else statement - that is, I'd expect a sequence of at least 4 different if-else statements.
  • The Senate elections are the hardest to figure out because of the two different senate schedules. There are several ways you could handle this. One way to do it might be to use a nested conditional.

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.