Lab 7: Making Decisions

This lab will help you get comfortable with logic and if/else statements.

Part 1: Practice with Conditions

This part will get you started writing the condition after the if keyword. Before you start this section run the code in the cell below to setup values to work with in the rest of the section.

[ ]:
course = "Python Programming for Everyone"
course_number = "cis-15"
instructor = "Mike"
year = 2020

Now fill in the variables in this cell with information about you and execute the cell:

[ ]:
first_name = ""
birth_year = 2000
favorite_number = 0

1.1. Print “Low” if your favorite_number is less than 100

[ ]:

1.2. Print “Snakes!” if course contains the word “Python”

[ ]:

1.3. Print “No Snakes!” if course does not contain the word “Python”

[ ]:

1.4. Print “Same” if you have the same first name is the instructor.

Use the instructor and first_name variables.

[ ]:

1.5. Print “VOTE!” if you are 18 years old or older.

Use the year and birth_year variables.

[ ]:

1.6. Print “Yes” if your name starts with the letter “L”, “No” otherwise.

Use the first_name variable.

Hint: Google “Python string startswith”

[ ]:

1.7. Are you a Millenial?

Print “Millenial” if birth_year is between 1981 and 1996 (including those years). Print “older” if you are older and “younger” if you are younger than a Millenial.

[ ]:

Part 2: More Complicated Conditions

This part will challenge you to create more complicated if statements with elif.

2.1. Talking About Your Generation

Build on your Millenial program with information from Wikipedia. According to the page here’s what generation people belong to:

  • Baby Boomers: 1946 - 1964

  • Generation X: 1965 - 1980

  • Millenials: 1981 - 1996

  • Generation Z: 1997 - 2012

My apologies if you’re older than 75 or younger than 10. I didn’t mean to exclude you, only limit how much people had to type for this question! Write a program that prints a generation based on birth_year.

[ ]:

Be sure to test your program by changing ``birth_year``!

2.2. Reacting to input

Write a program that asks the user to type a word.

  • If the word starts with “X” or “Y”, print “=== X, Y ===”

  • If the word starts with “M” or “N”, print “=== M, N ===”

  • If the word equals “Mike”, print “=== Instructor ===”

  • If the word equals your name, print “=== Me ===”

  • If the word doesn’t match any of the above, print “=== No Match ===”

[ ]:

2.3. Draw a Shape

Write a program that draws a triangle or a square based on user input. Your program should use the input function to ask the user what figure to draw. Draw the figure they request. If you don’t recoginize the figure print an error message.

[ ]:

2.4. Improved Shape

Improve your program from the last part and have it ask two questions: What figure to draw and the lenght of each side.

[ ]:

Part 3: Decisions in Functions

Now let’s combine if statements and functions. This is a powerful combinataion so it’s good to get practice!

3.1. Convert your Generation Answer

Start with the answer to question 2.1. Put the code into a function called get_generation that takes one argument year and returns the generation of a person born in that year.

  • Name: get_generation

  • Arguments:

    • year (int) - The year a person was born

  • Returns: (string) - The generation a person is in

[ ]:

3.2. Compute a Letter Grade

Write a function called grade_letter_percent that takes one input percent and returns the grade letter given for that percentage.

  • Name: grade_letter_percent

  • Arguments:

    • percent - A grade percentage.

  • Returns:

    • The grade letter (e.g. “A”, “B”, “C”)

[ ]:

3.3. Improved Letter Grade

Improve your grade_letter_percent function so that it calculates the percentage based on a person’s total score in points and the points possible on an assignment.

  • Name: grade_letter

  • Arguments:

    • score_points - The grade for an assignment in points

    • total_points - The total points available for the assignment

  • Returns:

    • The grade letter (e.g. “A”, “B”, “C”)

[ ]:

3.4. Plus and Minus

Improve your grade_letter_percent function again to include + and - grades. A + grade is given to the highest percent grade in the category, for example 99% is an A+, 89% is a B+, etc. A - grade is given to the lowest grade in the category, for example 90% is an A-, 80% is a B-, etc.

  • Name: grade_letter_plus

  • Arguments:

    • grade - The grade in points

    • total - The total points for the assignment

  • Returns:

    • The grade letter (e.g. “A+”, “A”, “A-” “B+”)

[ ]: