Skip to main content

Section 2.10 Unit 2 Selection Coding Practice Exercises

90 minutes
These are exercises mostly written by Dr. Barb Ericson in the AP CSP and Python for Everybody ebooks on Runestone.
Data: unittesthelper.py
testvar = 5
def contains(item, container):
    return item if item in container else None

Activity 2.10.1.

The following code checks if an input email is valid. Run the code twice to test it once with a valid email and once with an invalid email. Change the code to check to work for other domains than "gmail.com" by just checking for @ and . in the email.

Activity 2.10.2.

Fix the indentation errors in the code, and change it to use only one if/else statement. The code should print β€œThe number is 5” when the number is 5, and should print β€œThe number is NOT 5” when it is not.

Activity 2.10.3.

Fix 3 indentation errors in the code below, and change it to use only one if/else statement. The code below is supposed to set the price to 1.5 if the weight is less than 2 and otherwise set it to 1.3. It then calculates the total by multiplying the weight by the price.

Activity 2.10.4.

To graduate from a particular high school, a student needs 22 or more total credits and at least 3 math credits. Given the number of totalCredits earned and the number of mathCredits, print out "Can Graduate" or "Cannot Graduate".

Activity 2.10.5.

Write code to check if a number is even or odd. An even number has a 0 remainder when divided by 2. Remember that you can use the modulo operator % to get the remainder of a division. number % 2 equals 0 if the number is even and 1 if it is odd.

Activity 2.10.6.

Write code to calculate pay given the number of hours worked and an hourly pay rate. If the hours worked are less than or equal to 40, it should just return the hours times the hourly pay. If more than 40 hours were worked, the person should earn normal pay for 40 hours and 1.5 times the normal pay for the hours over 40. So if someone worked 50 hours, at $18 per hour they would receive \(40 \cdot 18 + ((50 - 40) \cdot 18) \cdot 1.5\)

Activity 2.10.7.

Write code to determine the class level based on the number of credits someone has taken. A Senior has taken 90 or more credits. A Junior hasn’t taken that many credits, but has taken at least 60. A Sophomore has taken at least 30 credits. A First Year is anyone who has taken 30 or less.
Write code to to figure out the class level based on the number of credits. Your code should use credits and the above logic to print: "Senior", "Junior", "Sophomore", or "First Year".

Activity 2.10.8.

The program below is supposed to calculate the final bill for a meal at a fast-food restaurant in a state where meals that are taken to go are not taxed, but meals eaten in the restaurant are.
Your job is to calculate the total cost given the initial mealPrice and the mealLocation. If mealLocation has the string "to go" anywhere in it, the total cost should be just the meal price. Otherwise, it should be the price + 8% (1.08 times the meal price).
Remember that you can use in to check if one string appears inside another.

Activity 2.10.9.

The program below is supposed to figure out the cost of a ticket for a customer to a museum. Anyone who is at least 65 or who is younger than 12 gets in free. Anyone who is 12 through 17 gets in for $8. Everyone else is $15.
You have attempted of activities on this page.