SolutionsChapter 3.docx
- 文档编号:28153895
- 上传时间:2023-07-08
- 格式:DOCX
- 页数:11
- 大小:16.50KB
SolutionsChapter 3.docx
《SolutionsChapter 3.docx》由会员分享,可在线阅读,更多相关《SolutionsChapter 3.docx(11页珍藏版)》请在冰豆网上搜索。
SolutionsChapter3
Solutions-Chapter3
3-1:
Names
Storethenamesofafewofyourfriendsinalistcalled names.Printeachperson’snamebyaccessingeachelementinthelist,oneatatime.
names=['ron','tyler','dani']
print(names[0])
print(names[1])
print(names[2])
Output:
ron
tyler
dani
3-2:
Greetings
StartwiththelistyouusedinExercise3-1,butinsteadofjustprintingeachperson’sname,printamessagetothem.Thetextofeachmessageshouldbethesame,buteachmessageshouldbepersonalizedwiththeperson’sname.
names=['ron','tyler','dani']
msg="Hello,"+names[0].title()+"!
"
print(msg)
msg="Hello,"+names[1].title()+"!
"
print(msg)
msg="Hello,"+names[2].title()+"!
"
print(msg)
Output:
Hello,Ron!
Hello,Tyler!
Hello,Dani!
3-4:
GuestList
Ifyoucouldinviteanyone,livingordeceased,todinner,whowouldyouinvite?
Makealistthatincludesatleastthreepeopleyou’dliketoinvitetodinner.Thenuseyourlisttoprintamessagetoeachperson,invitingthemtodinner.
guests=['guidovanrossum','jackturner','lynnhill']
name=guests[0].title()
print(name+",pleasecometodinner.")
name=guests[1].title()
print(name+",pleasecometodinner.")
name=guests[2].title()
print(name+",pleasecometodinner.")
Output:
GuidoVanRossum,pleasecometodinner.
JackTurner,pleasecometodinner.
LynnHill,pleasecometodinner.
3-5:
ChangingGuestList
Youjustheardthatoneofyourguestscan’tmakethedinner,soyouneedtosendoutanewsetofinvitations.You’llhavetothinkofsomeoneelsetoinvite.
∙StartwithyourprogramfromExercise3-4.Adda print statementattheendofyourprogramstatingthenameoftheguestwhocan’tmakeit.
∙Modifyyourlist,replacingthenameoftheguestwhocan’tmakeitwiththenameofthenewpersonyouareinviting.
∙Printasecondsetofinvitationmessages,oneforeachpersonwhoisstillinyourlist.
#Invitesomepeopletodinner.
guests=['guidovanrossum','jackturner','lynnhill']
name=guests[0].title()
print(name+",pleasecometodinner.")
name=guests[1].title()
print(name+",pleasecometodinner.")
name=guests[2].title()
print(name+",pleasecometodinner.")
name=guests[1].title()
print("\nSorry,"+name+"can'tmakeittodinner.")
#Jackcan'tmakeit!
Let'sinviteGaryinstead.
del(guests[1])
guests.insert(1,'garysnyder')
#Printtheinvitationsagain.
name=guests[0].title()
print("\n"+name+",pleasecometodinner.")
name=guests[1].title()
print(name+",pleasecometodinner.")
name=guests[2].title()
print(name+",pleasecometodinner.")
Output:
GuidoVanRossum,pleasecometodinner.
JackTurner,pleasecometodinner.
LynnHill,pleasecometodinner.
Sorry,JackTurnercan'tmakeittodinner.
GuidoVanRossum,pleasecometodinner.
GarySnyder,pleasecometodinner.
LynnHill,pleasecometodinner.
3-6:
MoreGuests
Youjustfoundabiggerdinnertable,sonowmorespaceisavailable.Thinkofthreemoregueststoinvitetodinner.
∙StartwithyourprogramfromExercise3-4orExercise3-5.Adda print statementtotheendofyourprograminformingpeoplethatyoufoundabiggerdinnertable.
∙Use insert() toaddonenewguesttothebeginningofyourlist.
∙Use insert() toaddonenewguesttothemiddleofyourlist.
∙Use append() toaddonenewguesttotheendofyourlist.Printanewsetofinvitationmessages,oneforeachpersoninyourlist.
#Invitesomepeopletodinner.
guests=['guidovanrossum','jackturner','lynnhill']
name=guests[0].title()
print(name+",pleasecometodinner.")
name=guests[1].title()
print(name+",pleasecometodinner.")
name=guests[2].title()
print(name+",pleasecometodinner.")
name=guests[1].title()
print("\nSorry,"+name+"can'tmakeittodinner.")
#Jackcan'tmakeit!
Let'sinviteGaryinstead.
del(guests[1])
guests.insert(1,'garysnyder')
#Printtheinvitationsagain.
name=guests[0].title()
print("\n"+name+",pleasecometodinner.")
name=guests[1].title()
print(name+",pleasecometodinner.")
name=guests[2].title()
print(name+",pleasecometodinner.")
#Wegotabiggertable,solet'saddsomemorepeopletothelist.
print("\nWegotabiggertable!
")
guests.insert(0,'fridakahlo')
guests.insert(2,'reinholdmessner')
guests.append('elizabethperatrovich')
name=guests[0].title()
print(name+",pleasecometodinner.")
name=guests[1].title()
print(name+",pleasecometodinner.")
name=guests[2].title()
print(name+",pleasecometodinner.")
name=guests[3].title()
print(name+",pleasecometodinner.")
name=guests[4].title()
print(name+",pleasecometodinner.")
name=guests[5].title()
print(name+",pleasecometodinner.")
Output:
GuidoVanRossum,pleasecometodinner.
JackTurner,pleasecometodinner.
LynnHill,pleasecometodinner.
Sorry,JackTurnercan'tmakeittodinner.
GuidoVanRossum,pleasecometodinner.
GarySnyder,pleasecometodinner.
LynnHill,pleasecometodinner.
Wegotabiggertable!
FridaKahlo,pleasecometodinner.
GuidoVanRossum,pleasecometodinner.
ReinholdMessner,pleasecometodinner.
GarySnyder,pleasecometodinner.
LynnHill,pleasecometodinner.
ElizabethPeratrovich,pleasecometodinner.
3-7:
ShrinkingGuestList
Youjustfoundoutthatyournewdinnertablewon’tarriveintimeforthedinner,andyouhavespaceforonlytwoguests.
∙StartwithyourprogramfromExercise3-6.Addanewlinethatprintsamessagesayingthatyoucaninviteonlytwopeoplefordinner.
∙Use pop() toremoveguestsfromyourlistoneatatimeuntilonlytwonamesremaininyourlist.Eachtimeyoupopanamefromyourlist,printamessagetothatpersonlettingthemknowyou’resorryyoucan’tinvitethemtodinner.
∙Printamessagetoeachofthetwopeoplestillonyourlist,lettingthemknowthey’restillinvited.
∙Use del toremovethelasttwonamesfromyourlist,soyouhaveanemptylist.Printyourlisttomakesureyouactuallyhaveanemptylistattheendofyourprogram.
#Invitesomepeopletodinner.
guests=['guidovanrossum','jackturner','lynnhill']
name=guests[0].title()
print(name+",pleasecometodinner.")
name=guests[1].title()
print(name+",pleasecometodinner.")
name=guests[2].title()
print(name+",pleasecometodinner.")
name=guests[1].title()
print("\nSorry,"+name+"can'tmakeittodinner.")
#Jackcan'tmakeit!
Let'sinviteGaryinstead.
del(guests[1])
guests.insert(1,'garysnyder')
#Printtheinvitationsagain.
name=guests[0].title()
print("\n"+name+",pleasecometodinner.")
name=guests[1].title()
print(name+",pleasecometodinner.")
name=guests[2].title()
print(name+",pleasecometodinner.")
#Wegotabiggertable,solet'saddsomemorepeopletothelist.
print("\nWegotabiggertable!
")
guests.insert(0,'fridakahlo')
guests.insert(2,'reinholdmessner')
guests.append('elizabethperatrovich')
name=guests[0].title()
print(name+",pleasecometodinner.")
name=guests[1].title()
print(name+",pleasecometodinner.")
name=guests[2].title()
print(name+",pleasecometodinner.")
name=guests[3].title()
print(name+",pleasecometodinner.")
name=guests[4].title()
print(name+",pleasecometodinner.")
name=guests[5].title()
print(name+",pleasecometodinner.")
#Ohno,thetablewon'tarriveontime!
print("\nSorry,wecanonlyinvitetwopeopletodinner.")
name=guests.pop()
print("Sorry,"+name.title()+"there'snoroomatthetable.")
name=guests.pop()
print("Sorry,"+name.title()+"there'snoroomatthetable.")
name=guests.pop()
print("Sorry,"+name.title()+"there'snoroomatthetable.")
name=guests.pop()
print("Sorry,"+name.title()+"there'snoroomatthetable.")
#Thereshouldbetwopeopleleft.Let'sinvitethem.
name=guests[0].title()
print(name+",pleasecometodinner.")
name=guests[1].title()
print(name+",pleasecometodinner.")
#Emptyoutthelist.
del(guests[0])
del(guests[0])
#Provethelistisempty.
print(guests)
Output:
GuidoVanRossum,pleasecometodinner.
JackTurner,pleasecometodinner.
LynnHill,pleasecometodinner.
Sorry,JackTurnercan'tmakeittodinner.
GuidoVanRossum,pleasecometodinner.
GarySnyder,pleasecometodinner.
LynnHill,pleasecometodinner.
Wegotabiggertable!
FridaKahlo,pleasecometodinner.
GuidoVanRossum,pleasecometodinner.
ReinholdMessner,pleasecometodinner.
GarySnyder,pleasecometodinner.
LynnHill,pleasecometodinner.
ElizabethPeratrovich,pleasecometodinner.
Sorry,wecanonlyinvitetwopeopletodinner.
Sorry,ElizabethPeratrovichthere'snoroomatthetable.
Sorry,LynnHillthere'snoroomatthetable.
Sorry,GarySnyderthere'snoroomatthetable.
Sorry,ReinholdMessnerthere'snoroomatthetable.
FridaKahlo,pleasecometodinner.
GuidoVanRossum,pleasecometodinner.
[]
3-8:
SeeingtheWorld
Thinkofatleastfiveplacesintheworldyou’dliketovisit.
∙Storethelocationsinalist.Makesurethelistisnotinalphabeticalorder.
∙Printyourlistinitsoriginalorder.Don’tworryaboutprintingthelistneatly,justprintitasarawPythonlist.
∙Use sorted() toprintyourlistinalphabeticalorderwithoutmodifyingtheactuallist.
∙Showthatyourlistisstillinitsoriginalorderbyprintingit.
∙Use sorted() toprintyourlistinreversealphabeticalorderwithoutchangingtheorderoftheoriginallist.
∙Showthatyourlistisstillinitsoriginalorderbyprintingitagain.
∙Use reverse() tochangetheorderofyourlist.Printthelisttoshowthatitsorderhaschanged.
∙Use r
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- Solutions Chapter