Here is a quick outline of what will be covered on Exam #2. The list below contains only the new material covered since exam #1. You need to know all the material that we covered before exam #1 as well.
Repetition Structures
Sample Problems
Trace the output of the following programs (each code fragment below is a separate Python program.)
for x in [1,2,3,4,5]: print (x)
————
for x in [0, 5]: print (x)
————
for x in range(5): print (x)
————
for p in range(1,10): print (p)
————
for q in range(100,50,-10): print (q)
————
for z in range(-500,500,100): print (z)
————
for y in range(500,100,100): print ("*", y)
————
x = 10 y = 5 for i in range(x-y*2): print ("%", i)
————
for x in [1,2,3]: for y in [4,5,6]: print (x,y)
————
for x in range(3): for y in range(4): print (x,y,x+y)
————
c = 0 for x in range(10): for y in range(5): c += 1 print (c)
————
def fun1(): print ("hi!") def fun2(): print ("bye!") if 10 < 5 or 5 > 6: fun1() fun2() else: fun2() fun1()
————
def fun(a): print ("a=", a) for x in range(5): fun(x)
————
def fun2(a): print (a+100) c = 5 while c < 10: fun2(c) c+=1
————
def fun(a,b): c = a**b return c for x in range(2,4): for y in range(2,4): print (x,y, fun(x,y))
————
def check(x): if x >= 'a' and x <= 'm': return True else: return False for name in ['apple', 'pear', 'peach', 'apricot', 'starfruit']: result = check(name) if result: print (str.upper(name)) else: print (str.lower(name))
————
a = 1 def f1(b): global a x = b * a a += 1 return '*' * x for y in range(5): print (f1(y))
————
def render(a,b,c,d,e,f,g,h): for i in range(a): print (' ', end='') for j in range(b): print ('*', end='') for k in range(c): print (' ', end='') for m in range(d): print ('*', end='') for n in range(e): print (' ', end='') for o in range(f): print ('*', end='') for p in range(g): print (' ', end='') for q in range(h): print ('*', end='') print() render (10, 1, 0, 0, 0, 0, 0, 0) render (9, 1, 0, 0, 0, 0, 0, 0) render (6, 6, 0, 0, 0, 0, 0, 0) render (4, 2, 6, 2, 0, 0, 0, 0) render (3, 1, 3, 1, 3, 1, 2, 1) render (3, 1, 2, 3, 1, 3, 1, 1) render (3, 1, 10, 1, 0, 0, 0, 0) render (3, 1, 2, 1, 5, 1, 1, 1) render (3, 1, 3, 5, 2, 1, 0, 0) render (3, 1, 10, 1, 0, 0, 0, 0) render (4, 2, 6, 2, 0, 0, 0, 0) render (6, 6, 0, 0, 0, 0, 0, 0)
————
def f1(a): if a < 0: return 0 elif a < 2: return 1 elif a < 4: return 2 else: return 3 for x in range(5): print ('*' * f1(x))
————
Enter a number of days: 3 Day 1: Enter a weight: 180 Day 2: Enter a weight: 175 Day 3: Enter a weight: 170 Average weight for 3 days: 175.0
————
Enter a number of days: 3 Day 1: Enter a weight: 180 Day 2: Enter a weight: 0 Invalid, try again! Day 2: Enter a weight: -5 Invalid, try again! Day 2: Enter a weight: 175 Day 3: Enter a weight: 170 Average weight for 3 days: 175.0
————
myfun(5,2) >> 25
————
Buy between 1 and 10 fireworks, get 0 free Buy between 11 and 20 fireworks, get 1 free Buy between 21 and 30 fireworks, get 3 free Buy more than 31 fireworks, get 5 free
————
Write a function that accepts one argument (# of fireworks) and returns the number of free fireworks that should be given away with that order.
Enter a quantity: 100 You are eligible for 5 free fireworks! Would you like to calculate another quantity? (yes/no): yes Enter a quantity: 3 Sorry, you are not eligible for any free fireworks. Would you like to calculate another quantity? (yes/no): no
————
100-90: A 80-89.99: B 70-79.99: C 65-69.99: D Lower than 65: F
————
Enter a grade: 98 98 is a(n) A Enter a grade: 76 76 is a(n) C Enter a grade: -2
————
Solutions
Question #1
for i in range(2, 100001, 2): print (i)
Question #2
total = 0 again = "yes" while again == "yes": p = float(input("Enter a price: ")) total += p again = input("Enter another price? (yes or no): ")
Question #3
total = 0 days = int(input("Enter a number of days: ")) for day in range(days): prompt = "Day " + str(day) + ": Enter a weight: " weight = input(prompt) total += weight print ("Average weight for", days, "days:", total/days)
Question #4
total = 0 days = int(input("Enter a number of days: ")) for day in range(days): while True: prompt = "Day " + str(day) + ": Enter a weight: " weight = input(prompt) if weight > 0: total += weight break else: print ("Invalid, try again") print ("Average weight for", days, "days:", total/days)
Question #5
def myfun(a,b): print a**b myfun(5,2)
Questions #6 and #7
def get_num_free_fireworks(num): if num <= 10: return 0 elif num <= 20: return 1 elif num <= 30: return 3 else: return 5 while True: q = int(input("Enter a quantity: ")) free = get_num_free_fireworks(q) print ("You are eligible for", free, "free fireworks!") again = input("Would you like to run the program again?") if again == "no": break
Questions #8 and #9
def compute_grade(g): if g >= 90: return 'A' elif g >= 80: return 'B' elif g >= 70: return 'C' elif g >= 65: return 'D' else: return 'F' while True: g = int(input("Enter a grade: ")) if g > 100 or g < 0: break letter = compute_grade(g) print (g, "is a(n)", letter)