Home CPSC 110

Midterm Review

 

Terms and Ideas

Understand and be able to reasonably define the following terms:


 

Variables and I/O

What do the following programs print?

# assume the user enters 6
num = int(input("Enter a number: "))
total = num * 10
print(total)

# assume the user enters 'Dogs'
message = input("Enter a message: "))
print(message[3])

x = 7
y = x + 1
y = y * 2
z = x + y
print(z)

a = 3
b = 4
c = a + 2 * (b + 5)
print(c)




What is wrong with the following programs?

cost = input("Enter the cost: ")
tax = 0.053 * cost
print("Tax is" tax)

input("Enter your name: ")
print("Hello", name)

length = int(input("Enter length: "))
width = int(input("Enter width: ")
length * width = area
print(area)

age = 2019 - born
born = float(input("Enter year you were born: "))
print("You are about", age, "years old.")




Writing code
 

Conditionals

What do the following programs print?

name = input("What's your name? ")

if len(name) < 5:
    print("My you have a short name")
    print(name, "is a good name though!")
print("Thanks for running the program", name) 

x = 7
y = 2

if y != 2:
    print("Apple")
elif x == 7:
    print("Banana")
else:
    print("Pear")

temp = 72

if temp < 20:
    print("Super cold")
elif temp < 60:
    print("Cold")
elif temp < 80:
    print("Nice")
elif temp < 90:
    print("Warm")
else:
    print("Hot")

temp = 72

if temp < 20:
    print("Super cold")
if temp < 60:
    print("Cold")
if temp < 80:
    print("Nice")
if temp < 90:
    print("Warm")
else:
    print("Hot")

age = int(input("How old are you? "))
height = int(input("How tall are you? "))

if age > 10 and height > 48:
    print("You can ride the roller coaster")
else:
    print("Sorry you can't ride")

If the user enters:


# this is the same program, except the and is an or
age = int(input("How old are you? "))
height = int(input("How tall are you? "))

if age > 10 or height > 48:
    print("You can ride the roller coaster")
else:
    print("Sorry you can't ride")

If the user enters:





Writing programs
  • The program above checks if the user meets the height and age requirement of a roller coaster. If they can ride it tells them so. If they can't it tells them they can't ride, but not why. Write a version of it that tells them either they can ride, they are not old enough, or they are not tall enough.
  • You are helping organize a large event which people need to check into. To help make checking in faster, you are dividing people by last name. Those with a last name beginning with a letter A–F check in at line 1. Those with a first letter from G–L go to line 2. Those with M–R go to line 3, and those with a first letter from S–Z go to line 4. Write a program which will ask the user for their last name and tell them which line to go to.
  • At UMW, students graduating in even years are said to be "Goats". Students graduating in odd years are said to be "Devils". Write a program to ask the user what year they are graduating, and tells them if they are a Devil or a Goat.
  • It's harder than you probably think to determine if a year is a leap year or not — it's not just every 4 years. The rules are:
    1. If the year is divisible by 400 it's a leap year
    2. Else if it's divisible by 100 it's not a leap year
    3. Else if it's divisible by 4 it is a leap year
    4. Otherwise it's not a leap year
    Write a Python program to read in a year and tell the user whether it's a leap year or no.

  •  

    Loops

    What do the following programs print?
    
    # assume the user enters the numbers "7 3 2 -1"
    value = float(input("Enter a number: "))
    prod = 1
    while value > 0:
        prod = prod * value
        value = float(input("Enter a number: "))
    print(prod)
    
    
    number = 7
    times = 0
    
    while number > 0:
        number = number - 2
        times = times + 1
    print(times)
    
    
    # assume the user enters 5
    start = int(input("Enter start: "))
    for num in range(start, 0, -1):
        print(num, "...")
    print("Blastoff!")
    
    
    # assume the user enters 5
    end = int(input("Enter end: "))
    
    tot = 0
    for i in range(1, val + 1):
        tot = tot + i
    print(tot)
    
    
    
    
    
    
    Writing Programs
  • Write a program that reads in a number from the user, and a message. The program should then print the message out "number" many times. For example, if they enter 3 and "Hello", then the program should print "Hello" three times.
  • Write a program which will let the user print the length of strings, for as many as they want. It should ask them to enter a string and then print the length of it. It should keep doing this until they enter an empty string (with a length of 0). Then the program should end.
  • Copyright © 2024 Ian Finlayson | Licensed under a Attribution-NonCommercial 4.0 International License.