Repository navigation
Expand file tree
/
Copy pathchapter_3.py
More file actions
139 lines (86 loc) · 5.04 KB
/
Copy pathchapter_3.py
File metadata and controls
139 lines (86 loc) · 5.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# NOTE: This script is not finished yet (Exercises 3-7 and 3-8 are incomplete).
"""Store the names of a few of your friends in a list called names. Print
each person’s name by accessing each element in the list, one at a time."""
def print_friend_names(friend_names):
for friend in friend_names:
print(friend)
people_i_know = ["Kzar", "Ari", "Morgan"]
print_friend_names(people_i_know)
"""Start with the list you used in Exercise 3-1, but instead of just
printing each person’s name, print a message to them. The text of each message should be the same,
but each message should be personalized with the
person’s name"""
def print_friend_messages(friend_names):
for friend in friend_names:
print(f"{friend} is a good friend of mine.")
people_i_know = ["Kzar", "Ari", "Morgan"]
print_friend_messages(people_i_know)
"""Think of your favorite mode of transportation, such as a
motorcycle or a car, and make a list that stores several examples. Use your list
to print a series of statements about these items, such as “I would like to own a
Honda motorcycle."""
def print_dream_cars(cars):
for car in cars:
print(f"I would love to own a {car} one day.")
dream_cars = ["Barbus G Wagon", "Shelby Truck", "Nissan GTR"]
print_dream_cars(dream_cars)
"""If you could invite anyone, living or deceased, to dinner, who
would you invite?
Make a list that includes at least three people you’d like to invite to dinner.
Then use your list to print a message to each person, inviting them to dinner."""
def send_dinner_invitations(guests):
for guest in guests:
print(f"Dear {guest}, please have dinner with me. Thanks.")
dinner_guests = ["Micheal Jackson", "God", "The World's Richest Man in the World"]
send_dinner_invitations(dinner_guests)
"""You just heard that one of your guests can’t make the
dinner, so you need to send out a new set of invitations. You’ll have to think of
someone else to invite.
• Start with your program from Exercise 3-4. Add a print() call at the end of
your program, stating the name of the guest who can’t make it.
• Modify your list, replacing the name of the guest who can’t make it with the
name of the new person you are inviting.
• Print a second set of invitation messages, one for each person who is still in
your list"""
guest_not_coming = dinner_guests.pop(1)
print(f"{guest_not_coming} is not coming to dinner.")
dinner_guests.insert(1, "Jesus Christ")
"""You just found a bigger dinner table, so now more space is
available. Think of three more guests to invite to dinner.
• Start with your program from Exercise 3-4 or 3-5. Add a print() call to the
end of your program, informing people that you found a bigger table.
• Use insert() to add one new guest to the beginning of your list.
• Use insert() to add one new guest to the middle of your list.
• Use append() to add one new guest to the end of your list.
• Print a new set of invitation messages, one for each person in your list"""
print("I found a bigger table!")
dinner_guests.insert(0, "Anakin Skywalker")
dinner_guests.insert(2, "Malcolm X")
dinner_guests.append("Random Russain guy")
send_dinner_invitations(dinner_guests)
"""You just found out that your new dinner table won’t
arrive in time for the dinner, and now you have space for only two guests.
• Start with your program from Exercise 3-6. Add a new line that prints a
message saying that you can invite only two people for dinner.
• Use pop() to remove guests from your list one at a time until only two
names remain in your list. Each time you pop a name from your list, print a
message to that person letting them know you’re sorry you can’t invite them
to dinner.
• Print a message to each of the two people still on your list, letting them
know they’re still invited.
• Use del to remove the last two names from your list, so you have an empty
list. Print your list to make sure you actually have an empty list at the end of
your program"""
"""Think of at least five places in the world you’d like to visit.
• Store the locations in a list. Make sure the list is not in alphabetical order.
• Print your list in its original order. Don’t worry about printing the list neatly;
just print it as a raw Python list.
• Use sorted() to print your list in alphabetical order without modifying the
actual list.
• Show that your list is still in its original order by printing it.
• Use sorted() to print your list in reverse-alphabetical order without changing the order of the original list.
• Show that your list is still in its original order by printing it again.
• Use reverse() to change the order of your list. Print the list to show that its order has changed.
• Use reverse() to change the order of your list again. Print the list to show it’s back to its original order.
• Use sort() to change your list so it’s stored in alphabetical order. Print the list to show that its order has been changed.
• Use sort() to change your list so it’s stored in reverse-alphabetical order. Print the list to show that its order has changed."""