3. Selections

Lecture 3.

Selections

94 minutes video lecture, 35 minutes coding practice time.

Slides

Video Lectures

Review Questions

1: if statement must be accompanied by else statement. (T/F)?

False

2: What does the following program print?

radius = 7.5
if radius > 7:
    print(radius)
Click here to see the answer.

7.5

3: What does the following program print?

radius = 8
if radius > 8:
    print(radius)
else:
    radius = 9
Click here to see the answer.

This program does not print anything.

4: What does the following program print?

radius = 8
if radius != 8:
    print(radius)
else:
    radius = 9
    print(radius)
Click here to see the answer.

9

5: What does the following program print?

b = 1 > 2
if b::
    print(b)
Click here to see the answer.

This program does not print anything.

6: What does the following program print?

score = 75
if score > 70:
    print(score)
elif score > 65:
    score += 10
    print(score)
Click here to see the answer.

75

7: What does the following program print?

score = 75
if score > 70:
    print(score)
if score > 65:
    score += 10
    print(score)
Click here to see the answer.

75 85

8: What does the following program print?

score = 75
if score > 70:
    if score < 60:
        print(score)
else:
    score += 10
    print(score)
Click here to see the answer.

This program does not print anything.

9: What does the following program print?

score = 75
if score > 70:
    if score < 60:
        print(score)
    else:
        score += 10
        print(score)
Click here to see the answer.

85

10: What does the following program print?

score = 75
age = 19
height = 181
if age > 19:
    print (score)
elif not age > 18:
    score += 10
    print(score)
Click here to see the answer.

This program does not print anything.

11: What does the following program print?

score = 75
age = 19
height = 181
if age > 19 or height < 190:
    print (score)
elif age > 18:
    score += 10
    print(score)
Click here to see the answer.

75

12: What does the following program print?

score = 75
age = 19
height = 181
if age > 18 and height < 180:
    print (score)
elif age > 19 or height > 190:
    score += 10
    print(score)
Click here to see the answer.

This program does not print anything.

13: What does the following program print?

score = 75
age = 19
height = 181
if not age > 19 and height < 180:
    print (score)
elif not age > 18 or height > 190:
    score += 10
    print(score)
Click here to see the answer.

85

14: What does the following program print?

score = 75
age = 19
height = 181
if not age > 19 and height < 180:
    print (score)
elif not (age > 18 or height > 190):
    score += 10
    print(score)
Click here to see the answer.

This program does not print anything.

15: What does the following program print?

score = 75
age = 19
height = 181
if not (age > 19 and height < 180):
    print (score)
elif not (age > 18 or height > 190):
    score += 10
    print(score)
Click here to see the answer.

75

In-class Programming Exercises

1: Write a program that randomly generates two single digit integers, and displays a question such as “ What is 3 + 5?”. Then, the program reads in the answer from the user, and displays a message to indicate whether the answer is correct or not.

Hint: To generate a random single digit integer, you may use the following code.

import random
number = random. randint (0,9)
Click to see the answer.

# In-class Exercise 1

import random
# Step 1: Randomly generate two numbers
number1 = random.randint (0, 9)
number2 = random.randint (0, 9)

# Step 2: Display the question and read in the answer
print("What is", number1, "+", number2, "?")
answer = int ( input("Please type the answer and press Enter:"))

# Step 3: Check whether the answer is correct or not
if answer == number1 + number2:
    print("Your answer is correct!")
else:
    print("Your answer is wrong!")

2: Write a program that prompts the user to enter an integer for a day of the week. Then, the program checks whether the corresponding day is a weekday or weekend, and displays the result appropriately.

Click to see the answer.

# In-class Exercise 2

# Step 1: Read in the day of the week from the user
dayOfTheWeek = int(input("Please input the day of the week (1 for Monday, ..., 7 for Sunday) and press Enter: "))

# Step 2: Check whether it is a weekday or a weekend
if dayOfTheWeek <= 5 and dayOfTheWeek >= 1:
    print("It's a weekday!")
elif dayOfTheWeek == 6 or dayOfTheWeek == 7:
    print("It's a weekend!")
else:
    print("Invalid input!")

3: Write a program that prompts the user to enter a year as an integer, and checks whether it is a leap year. Hint: A year is a leap year if (1) it is divisible by 400, or (2a) it is divisible by 4 and (2b) not divisible by 100.

Click to see the answer.

# In-class Exercise 3

# Step 1: Read in the year
year = int( input ("Please input the year and press Enter:"))

# Step 2: Check whether the year is a leap year
if year % 400 == 0 or (year % 4 == 0 and not year % 100 == 0):
    print("It’s a leap year!")
else:
    print("It’s not a leap year!")

Practice-at-home Programming Exercises

1: Write a program that prompts the user to enter a movie’s IMDB rating (0 to 10 - may include decimal, like 3.5) and Metascore (0 to 100 - integer), and checks whether the movie is recommended to watch.

Hint: Recommend if rating > 7.0 and Metascore > 60.

Click to see the answer.

# Practice Exercise 1

# Step 1: Read in the IMDB rating and metascore
IMDBRating = float(input("Please input IMDB rating of a movie (0 to 10) and press Enter:"))
metascore = int(input("Please input IMDB rating of a movie (0 to 100 - integer) and press Enter:"))

# Step 2: Recommend the movie if the rating > 7.0 and metascore > 60
if IMDBRating > 7.0 and metascore > 60:
    print("This movie is recommended to watch!")
else:
    print("This movie is not recommended to watch!")

2: Write a program that prompts the user to enter the day, month and year he/shewas born, and displays whether he/she can legally purchase beer in US.

Example dialogue:

  • Give me a beer, please.
  • Can I see an ID? 6.12.2002
  • I’m sorry, but I cannot sell you a beer.
Click to see the answer.

# Practice Exercise 2

# Step 1: Read in the day, month and year
print("Give me a beer please!")
print("Can I see an ID?")
dayOfBirth = int(input("Please input the day of your birthday and press Enter:"))
monthOfBirth = int(input("Please input the month of your birthday and press Enter:"))
yearOfBirth = int(input("Please input the year of your birthday and press Enter:"))

# Step 2: Check whether the user can legally purchase beer in US
if yearOfBirth < 2002 or (yearOfBirth == 2002 and monthOfBirth < 09) or (yearOfBirth == 2002 and monthOfBirth == 09 and dayOfBirth <= 10):
    print("Here is your beer!")
else:
    print("I'm sorry, I cannot sell you a beer!")

Answers to Programming Exercises

Feedback

Previous
Next