Midterm Review
Terms and Ideas
Understand and be able to reasonably define the following terms:
- algorithm
- computer
- program
- machine code
- assembly
- high-level language
- interpreter
- integrated development environment (IDE)
- shell
- variable
- type
- string
- int
- float
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
- Write a program to convert from feet to meters. There are 3.28084 feet in one meter. First read in the number of feet, do the calculation to find how many meters that is, and then print the result.
- Write a program to read in the radius of a circle from the user and print out the area. The area of a circle can be computed as $A = \pi \cdot R^2$
- Write a program to print the average of 4 numbers that the user gives. You should read in the 4 numbers, compute the average, and then print the answer.
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)
- When the user enters "Bob"?
- When the user enters "Montgomery"?
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:
- 34 and 72
- 9 and 40
- 12 and 48
- 8 and 50
# 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:
- 34 and 72
- 9 and 40
- 12 and 48
- 8 and 50
Writing programs
- If the year is divisible by 400 it's a leap year
- Else if it's divisible by 100 it's not a leap year
- Else if it's divisible by 4 it is a leap year
- Otherwise it's not a leap year
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