Skip to content

Commit 2d81779

Browse files
authored
Merge pull request UWPCE-PythonCert#75 from ea8606/master
session 3 for review
2 parents 78c2b04 + 3ff510d commit 2d81779

12 files changed

Lines changed: 493 additions & 0 deletions
Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
#!/usr/bin/env python
2+
3+
# Series 1
4+
5+
6+
def display_list(seq):
7+
"""Print each item in seq in a numbered list
8+
"""
9+
i = 0 # item number in displaying fruit list
10+
for x in seq:
11+
i += 1
12+
print(i, '. ', x)
13+
14+
15+
def print_what_was_entered(number, fruit):
16+
"""Print a statement saying the number and item chosen by the user
17+
"""
18+
print("You entered {}, which is {}".format(number, fruits[number - 1]))
19+
20+
21+
# Create a list that contains “Apples”, “Pears”, “Oranges” and “Peaches”.
22+
print("\nSeries 1\n")
23+
fruits = ['Apples', 'Pears', 'Oranges', 'Peaches']
24+
fruits_orig = fruits[:]
25+
# Display the list (plain old print() is fine…).
26+
print(fruits)
27+
# Ask the user for another fruit and add it to the end of the list.
28+
answer = input("Input a fruit to add to this list> ")
29+
fruits.append(answer.strip().title())
30+
# Display the list.
31+
display_list(fruits)
32+
# Ask the user for a number and display the number back to the user and the
33+
# fruit corresponding to that number (on a 1-is-first basis). Remember that
34+
# Python uses zero-based indexing, so you will need to correct.
35+
number = int(input(" Enter a number from 1 to " + str(len(fruits)) + "> "))
36+
print_what_was_entered(number, fruits)
37+
# Add another fruit to the beginning of the list using “+” and display the
38+
# list.
39+
answer = input("enter a fruit to add to the beginning of the list> ")
40+
fruits = [answer.strip().title()] + fruits
41+
display_list(fruits)
42+
# Add another fruit to the beginning of the list using insert() and display
43+
# the list.
44+
answer = input("> enter a fruit to add to the beginning of the list> ")
45+
fruits.insert(0, answer.strip().title())
46+
display_list(fruits)
47+
# Display all the fruits that begin with “P”, using a for loop.
48+
print("The following fruits begin with P:")
49+
for x in fruits:
50+
if x[0] == 'P':
51+
print(x)
52+
53+
# Series 2
54+
print("\nSeries 2\n")
55+
fruits = fruits_orig[:]
56+
# Using the list created in series 1 above:
57+
# Display the list.
58+
display_list(fruits)
59+
# Remove the last fruit from the list.
60+
print("Removing the last fruit from the list")
61+
fruits.remove(fruits[-1])
62+
# Display the list.
63+
display_list(fruits)
64+
# Ask the user for a fruit to delete and find it and delete it.
65+
answer = input("Enter the fruit you want to be deleted.> ")
66+
answer = answer.strip().title()
67+
print("You chose {}".format(answer))
68+
i = 0
69+
for x in fruits:
70+
if x == answer:
71+
fruits.remove(fruits[i])
72+
i += 1
73+
display_list(fruits)
74+
# (Bonus: Multiply the list times two. Keep asking until a match is found.
75+
# Once found, delete all occurrences.)
76+
print("Multipying the list by two")
77+
fruits = fruits * 2
78+
display_list(fruits)
79+
statement = "Enter the fruit you want to be deleted."
80+
while True:
81+
answer = input(statement)
82+
answer = answer.strip().title()
83+
print("You chose {}".format(answer))
84+
i = 0
85+
match = False
86+
for x in fruits:
87+
if x == answer:
88+
fruits.remove(fruits[i])
89+
match = True
90+
i += 1
91+
if match is True:
92+
break
93+
else:
94+
statement = answer + ' not found. Try again'
95+
display_list(fruits)
96+
97+
98+
# Series 3
99+
print("\n Series 3\n")
100+
# Again, using the list from series 1:
101+
fruits = fruits_orig[:]
102+
display_list(fruits)
103+
# Ask the user for input displaying a line like “Do you like apples?” for each
104+
# fruit in the list (making the fruit all lowercase).
105+
i = 0
106+
for x in fruits:
107+
answer = input('Do you like {}> '.format(x.lower()))
108+
# For each “no”, delete that fruit from the list.
109+
if answer.strip().lower() == 'no':
110+
print('Deleting {} from the list'.format(x.lower()))
111+
counter = 0
112+
for y in fruits:
113+
if fruits[counter] == x:
114+
fruits.remove(x)
115+
elif answer.strip().lower() == 'yes':
116+
continue
117+
# For any answer that is not “yes” or “no”, prompt the user to answer
118+
# with one of those two values (a while loop is good here)
119+
else:
120+
while True:
121+
answer = input('Please enter a "yes" or a "no"> ')
122+
if answer.strip().lower() == 'yes':
123+
break
124+
if answer.strip().lower() == 'no':
125+
break
126+
# Display the list.
127+
display_list(fruits)
128+
# Series 4
129+
print("\n Series 4\n")
130+
# Once more, using the list from series 1:
131+
print('List from Series 1:')
132+
fruits = fruits_orig[:]
133+
display_list(fruits)
134+
# Make a copy of the list and reverse the letters in each fruit in the copy.
135+
copy_fruits = fruits[:]
136+
print('Reversing the letters in each fruit, (copy_fruits')
137+
print("copy_fruits before reversing")
138+
print(copy_fruits)
139+
i = 0
140+
for x in copy_fruits:
141+
x = x[:: - 1]
142+
copy_fruits[i] = x
143+
i += 1
144+
print("copy_fruits after reversing\n", copy_fruits)
145+
# Delete the last item of the original list. Display the original list and
146+
# the copy.
147+
print("deleting the last item of fruits")
148+
del fruits[-1]
149+
print("Original list = {}\nCopy = {}".format(fruits, copy_fruits))
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
#!/usr/bin/env python
2+
3+
# Data
4+
donors_and_donations = ['Steve Jobs', [5, 10, 15], 'Bill Gates', [
5+
30, 40, 50], 'Barak Obama', [50], 'Donald Trump', [
6+
1, 2, 3], 'Melinda Gates', [10, 20, 30]]
7+
#
8+
# “Send a Thank You”, “Create a Report” or “quit”
9+
#
10+
11+
12+
def create_a_report():
13+
""" Print out a report of donors, total given number of donations
14+
and average donation
15+
"""
16+
donor_name = []
17+
donor_total_given = []
18+
donor_total_num_of_gifts = []
19+
total_given = 0.0
20+
average_gift = []
21+
list_of_donations = []
22+
average_of_gifts = 0
23+
print('{:<20}|{:^15}|{:^15}|{:^15}'.format(
24+
'Donor Name', 'Total Given', 'Num Gifts', 'Average Gift'))
25+
print('-' * 65)
26+
# Separate the individual donors and donations into separate lists
27+
i = 0
28+
while i < len(donors_and_donations):
29+
donor_name.append(donors_and_donations[i])
30+
list_of_donations.append(donors_and_donations[i + 1])
31+
i += 2
32+
# Calculate the sum of the gifts, the number of gifts, avg gift amount
33+
# for each donor, (each in a separate list)
34+
for donations in list_of_donations:
35+
# print(donations)
36+
# print(len(donations))
37+
i = 0
38+
total_given = 0
39+
# calculate the total given
40+
while i < len(donations):
41+
# print(donations[i])
42+
total_given += donations[i]
43+
i += 1
44+
average_of_gifts = total_given / len(donations)
45+
average_gift.append(average_of_gifts)
46+
donor_total_given.append(total_given)
47+
donor_total_num_of_gifts.append(len(donations))
48+
i = 0
49+
while i < len(donors_and_donations) // 2:
50+
print('{:<20}${:^15}${:^15}${:8.2f}'.format(
51+
donor_name[i], donor_total_given[i], donor_total_num_of_gifts[i],
52+
average_gift[i]))
53+
i += 1
54+
55+
56+
def compose_email(answer, answer1):
57+
""" Generate a one line email with donor name and amount contributed
58+
"""
59+
print('Thank you, {} for your generous donation of {}'.format
60+
(answer, answer1))
61+
62+
63+
def print_menu():
64+
"""Prompt the user to choose from a menu of 3 actions,
65+
'Send a Thank You', 'Create a Report' or 'quit'
66+
"""
67+
print("Menu\n1. Send a Thank You\n2. Create a Report\n3. Quit")
68+
answer = input("Enter a number between 1 and 3 (e.g 3) > ")
69+
answer = answer.strip()
70+
return answer
71+
72+
73+
def menu_thank_you():
74+
"""Prompt the user for a full list of donors or to update the current list
75+
"""
76+
# print(donors_and_donations)
77+
answer = input('Enter the full name of the donor or enter "list" for \
78+
the list of donors, or "quit" for main menu')
79+
return answer
80+
81+
82+
def send_a_thank_you():
83+
"""Update the donor list with donor name and amount donated.
84+
or print the list out
85+
"""
86+
answer = menu_thank_you()
87+
answer = answer.strip()
88+
answer1 = 0
89+
while True:
90+
if answer.lower() == 'list':
91+
print("The list of donors is: ")
92+
print(donors_and_donations[::2])
93+
answer = menu_thank_you()
94+
elif answer.lower() == 'quit':
95+
break
96+
# If donor is in the list, add the donation
97+
else:
98+
i = 0
99+
flag = 0
100+
for x in donors_and_donations:
101+
if x == answer:
102+
print('answer is ', answer)
103+
flag = 1
104+
answer1 = int(
105+
input("Enter the amount {} donated".format(x)))
106+
donors_and_donations[i + 1].append(answer1)
107+
print('donors_and_donations =', donors_and_donations)
108+
i += 1
109+
# donor not in the list
110+
if flag == 0:
111+
answer1 = int(
112+
input("Enter the amount {} donated".format(answer)))
113+
donors_and_donations.append(answer)
114+
donors_and_donations.append([answer1])
115+
compose_email(answer, answer1)
116+
answer = menu_thank_you()
117+
118+
119+
if __name__ == '__main__':
120+
answer = print_menu()
121+
while True:
122+
if answer == '3':
123+
break
124+
elif answer == '1':
125+
send_a_thank_you()
126+
answer = print_menu()
127+
elif answer == '2':
128+
create_a_report()
129+
answer = print_menu()
130+
else:
131+
print("You entered {}".format(answer))
132+
print("Menu\n1. Send a Thank You\n2. Create a Report\n3. Quit")
133+
answer = input("Enter a number between 1 and 3 (e.g 3) > ")
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# make a row that is goal inches log
2+
# 1" bricks and 5" bricks
3+
4+
def make_bricks(num_small, num_big, goal):
5+
if num_small * 1 + num_big * 5 < goal:
6+
return False
7+
needed_large = goal // 5
8+
needed_small = goal % 5
9+
10+
11+
print("needed_small:", needed_small)
12+
print("needed_large:", needed_large)
13+
14+
15+
16+
17+
18+
19+
20+
21+
22+
assert make_bricks(3, 1, 8) is True
23+
assert make_bricks(3, 1, 9) is False
24+
assert make_bricks(0, 2, 8) is False
25+
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
def elements_reversed(seq):
2+
""" Return a copy of a sequence with the elements reversed
3+
(just with slicing)
4+
"""
5+
new_seq = seq[-1:0:-1] + seq[0:1]
6+
return new_seq
7+
8+
9+
tuple_1 = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
10+
string_1 = 'I love Python'
11+
list_1 = [5, 8, 6, 10, 2, 4, 5, 15, 20]
12+
13+
assert elements_reversed(tuple_1) == (10, 9, 8, 7, 6, 5, 4, 3, 2, 1)
14+
assert elements_reversed(string_1) == 'nohtyP evol I'
15+
assert elements_reversed(list_1) == [20, 15, 5, 4, 2, 10, 6, 8, 5]
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
def every_other_item_removed(seq):
2+
""" Return a copy of a sequence with every other item
3+
removed.
4+
"""
5+
new_seq = seq[0::2]
6+
return new_seq
7+
8+
9+
tuple_1 = (1, 2, 3, 4)
10+
string_1 = ('I love Python')
11+
list_1 = [5, 8, 6, 10]
12+
13+
assert every_other_item_removed(tuple_1) == (1, 3)
14+
assert every_other_item_removed(string_1) == 'Ilv yhn'
15+
assert every_other_item_removed(list_1) == [5, 6]
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
def exchange_first_last(seq):
2+
"""Return a copy of a sequence with the first and last items
3+
exchanged.
4+
"""
5+
copy_seq = seq[-1:] + seq[1:-1] + seq[0:1]
6+
return copy_seq
7+
8+
9+
tuple_1 = (1, 2, 3, 4)
10+
string_1 = ('I love Python')
11+
list_1 = [5, 8, 6, 10]
12+
13+
assert exchange_first_last(tuple_1) == (4, 2, 3, 1)
14+
assert exchange_first_last(string_1) == 'n love PythoI'
15+
assert exchange_first_last(list_1) == [10, 8, 6, 5]
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
2+
def first_and_last_four_and_every_other_between(seq):
3+
""" Return a copy of a sequence with the first and
4+
last 4 items removed, and every other item in between.
5+
"""
6+
new_seq = seq[1:-4:2]
7+
return new_seq
8+
9+
tuple_1 = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
10+
string_1 = ('I love Python')
11+
list_1 = [5, 8, 6, 10, 2, 4, 5, 15, 20]
12+
13+
assert first_and_last_four_and_every_other_between(tuple_1) == (2, 4, 6)
14+
assert first_and_last_four_and_every_other_between(string_1) == ' oeP'
15+
assert first_and_last_four_and_every_other_between(list_1) == [8, 10]

0 commit comments

Comments
 (0)