1、Solutions Chapter 3Solutions - Chapter 33-1: NamesStore the names of a few of your friends in a list callednames. Print each persons name by accessing each element in the list, one at a time.names = ron, tyler, daniprint(names0)print(names1)print(names2)Output:rontylerdani3-2: GreetingsStart with th
2、e list you used in Exercise 3-1, but instead of just printing each persons name, print a message to them. The text of each message should be the same, but each message should be personalized with the persons name.names = ron, tyler, danimsg = Hello, + names0.title() + !print(msg)msg = Hello, + names
3、1.title() + !print(msg)msg = Hello, + names2.title() + !print(msg)Output:Hello, Ron!Hello, Tyler!Hello, Dani!3-4: Guest ListIf you could invite anyone, living or deceased, to dinner, who would you invite? Make a list that includes at least three people youd like to invite to dinner. Then use your li
4、st to print a message to each person, inviting them to dinner.guests = guido van rossum, jack turner, lynn hillname = guests0.title()print(name + , please come to dinner.)name = guests1.title()print(name + , please come to dinner.)name = guests2.title()print(name + , please come to dinner.)Output:Gu
5、ido Van Rossum, please come to dinner.Jack Turner, please come to dinner.Lynn Hill, please come to dinner.3-5: Changing Guest ListYou just heard that one of your guests cant make the dinner, so you need to send out a new set of invitations. Youll have to think of someone else to invite. Start with y
6、our program from Exercise 3-4. Add aprintstatement at the end of your program stating the name of the guest who cant make it. Modify your list, replacing the name of the guest who cant make it with the name of the new person you are inviting. Print a second set of invitation messages, one for each p
7、erson who is still in your list.# Invite some people to dinner.guests = guido van rossum, jack turner, lynn hillname = guests0.title()print(name + , please come to dinner.)name = guests1.title()print(name + , please come to dinner.)name = guests2.title()print(name + , please come to dinner.)name = g
8、uests1.title()print(nSorry, + name + cant make it to dinner.)# Jack cant make it! Lets invite Gary instead.del(guests1)guests.insert(1, gary snyder)# Print the invitations again.name = guests0.title()print(n + name + , please come to dinner.)name = guests1.title()print(name + , please come to dinner
9、.)name = guests2.title()print(name + , please come to dinner.)Output:Guido Van Rossum, please come to dinner.Jack Turner, please come to dinner.Lynn Hill, please come to dinner.Sorry, Jack Turner cant make it to dinner.Guido Van Rossum, please come to dinner.Gary Snyder, please come to dinner.Lynn H
10、ill, please come to dinner.3-6: More GuestsYou 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 Exercise 3-5. Add aprintstatement to the end of your program informing people that you found a
11、 bigger dinner table. Useinsert()to add one new guest to the beginning of your list. Useinsert()to add one new guest to the middle of your list. Useappend()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.# Invite some people to d
12、inner.guests = guido van rossum, jack turner, lynn hillname = guests0.title()print(name + , please come to dinner.)name = guests1.title()print(name + , please come to dinner.)name = guests2.title()print(name + , please come to dinner.)name = guests1.title()print(nSorry, + name + cant make it to dinn
13、er.)# Jack cant make it! Lets invite Gary instead.del(guests1)guests.insert(1, gary snyder)# Print the invitations again.name = guests0.title()print(n + name + , please come to dinner.)name = guests1.title()print(name + , please come to dinner.)name = guests2.title()print(name + , please come to din
14、ner.)# We got a bigger table, so lets add some more people to the list.print(nWe got a bigger table!)guests.insert(0, frida kahlo)guests.insert(2, reinhold messner)guests.append(elizabeth peratrovich)name = guests0.title()print(name + , please come to dinner.)name = guests1.title()print(name + , ple
15、ase come to dinner.)name = guests2.title()print(name + , please come to dinner.)name = guests3.title()print(name + , please come to dinner.)name = guests4.title()print(name + , please come to dinner.)name = guests5.title()print(name + , please come to dinner.)Output:Guido Van Rossum, please come to
16、dinner.Jack Turner, please come to dinner.Lynn Hill, please come to dinner.Sorry, Jack Turner cant make it to dinner.Guido Van Rossum, please come to dinner.Gary Snyder, please come to dinner.Lynn Hill, please come to dinner.We got a bigger table!Frida Kahlo, please come to dinner.Guido Van Rossum,
17、please come to dinner.Reinhold Messner, please come to dinner.Gary Snyder, please come to dinner.Lynn Hill, please come to dinner.Elizabeth Peratrovich, please come to dinner.3-7: Shrinking Guest ListYou just found out that your new dinner table wont arrive in time for the dinner, and you have space
18、 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. Usepop()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
19、a message to that person letting them know youre sorry you cant invite them to dinner. Print a message to each of the two people still on your list, letting them know theyre still invited. Usedelto remove the last two names from your list, so you have an empty list. Print your list to make sure you
20、actually have an empty list at the end of your program.# Invite some people to dinner.guests = guido van rossum, jack turner, lynn hillname = guests0.title()print(name + , please come to dinner.)name = guests1.title()print(name + , please come to dinner.)name = guests2.title()print(name + , please c
21、ome to dinner.)name = guests1.title()print(nSorry, + name + cant make it to dinner.)# Jack cant make it! Lets invite Gary instead.del(guests1)guests.insert(1, gary snyder)# Print the invitations again.name = guests0.title()print(n + name + , please come to dinner.)name = guests1.title()print(name +
22、, please come to dinner.)name = guests2.title()print(name + , please come to dinner.)# We got a bigger table, so lets add some more people to the list.print(nWe got a bigger table!)guests.insert(0, frida kahlo)guests.insert(2, reinhold messner)guests.append(elizabeth peratrovich)name = guests0.title
23、()print(name + , please come to dinner.)name = guests1.title()print(name + , please come to dinner.)name = guests2.title()print(name + , please come to dinner.)name = guests3.title()print(name + , please come to dinner.)name = guests4.title()print(name + , please come to dinner.)name = guests5.title
24、()print(name + , please come to dinner.)# Oh no, the table wont arrive on time!print(nSorry, we can only invite two people to dinner.)name = guests.pop()print(Sorry, + name.title() + theres no room at the table.)name = guests.pop()print(Sorry, + name.title() + theres no room at the table.)name = gue
25、sts.pop()print(Sorry, + name.title() + theres no room at the table.)name = guests.pop()print(Sorry, + name.title() + theres no room at the table.)# There should be two people left. Lets invite them.name = guests0.title()print(name + , please come to dinner.)name = guests1.title()print(name + , pleas
26、e come to dinner.)# Empty out the list.del(guests0)del(guests0)# Prove the list is empty.print(guests)Output:Guido Van Rossum, please come to dinner.Jack Turner, please come to dinner.Lynn Hill, please come to dinner.Sorry, Jack Turner cant make it to dinner.Guido Van Rossum, please come to dinner.G
27、ary Snyder, please come to dinner.Lynn Hill, please come to dinner.We got a bigger table!Frida Kahlo, please come to dinner.Guido Van Rossum, please come to dinner.Reinhold Messner, please come to dinner.Gary Snyder, please come to dinner.Lynn Hill, please come to dinner.Elizabeth Peratrovich, pleas
28、e come to dinner.Sorry, we can only invite two people to dinner.Sorry, Elizabeth Peratrovich theres no room at the table.Sorry, Lynn Hill theres no room at the table.Sorry, Gary Snyder theres no room at the table.Sorry, Reinhold Messner theres no room at the table.Frida Kahlo, please come to dinner.
29、Guido Van Rossum, please come to dinner.3-8: Seeing the WorldThink of at least five places in the world youd 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. Dont worry about printing the list neatly, just print it
30、as a raw Python list. Usesorted()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. Usesorted()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. Usereverse()to change the order of your list. Print the list to show that its order has changed. User