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

  1. Count Controlled Loops ("for" loops)
  2. The Range Function
  3. Functions

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))

————


Programming Problems

  1. Write a program that prints out all even numbers between 1 and 100,000.
  2. Write a program that continually asks the user for a series of prices. Keep track of the running total as the user enters prices. Prompt the user after each price to see if they wish to continue entering prices. When they are finished, print out the total amount entered.
  3. Write a program that asks the user for a number of days. Then prompt the user for their weight on each day they specified (i.e. if they enter 7 you should ask them for 7 weight values). When they are finished print out their average weight for the period in question. Sample running:
    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

    ————

  4. Re-write the previous program so that you re-prompt them if the user enters an invalid weight y(anything 0 or less). Ensure that you don't move to the next day without getting a valid weight. Sample running:
    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

    ————

  5. Write a function that accepts two arguments and computes the first argument raised to the power of the second argument. For example:
    myfun(5,2)
    
    >> 25

    ————

  6. You are working for a fireworks company that wants to try out a new marketing strategy. They would like to try a new promotion that gives away free fireworks to customers who purchase a certain quantity – the more you buy, the more free fireworks you get! Here is their current promotion:
    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.

  7. Write a program that prompts the user for an amount of fireworks. Then use your fireworks function that you wrote for the previous problem to figure out how many free fireworks they are eligible for. Print out a summary like the following and allow the user to continue entering quantities if desired.
    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

    ————

  8. Write a function that accepts a grade as a single floating point value and returns its letter equivalent. Letters grades can be determined using the following table:
    100-90: A
    80-89.99: B
    70-79.99: C
    65-69.99: D
    Lower than 65: F

    ————

  9. Write a program that continually asks the user for a grade value. Use the function you wrote for question #8 to determine the equivalent letter grade. You can assume the user is finished entering grades when they enter in an invalid number (i.e. one greater than 100 or less than 0). Here is a sample running of this program:
    Enter a grade:  98
    98 is a(n) A
    Enter a grade:  76
    76 is a(n) C
    Enter a grade:  -2

    ————

  10. 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)