SolutionsChapter 3.docx

上传人:b****5 文档编号:28153895 上传时间:2023-07-08 格式:DOCX 页数:11 大小:16.50KB
下载 相关 举报
SolutionsChapter 3.docx_第1页
第1页 / 共11页
SolutionsChapter 3.docx_第2页
第2页 / 共11页
SolutionsChapter 3.docx_第3页
第3页 / 共11页
SolutionsChapter 3.docx_第4页
第4页 / 共11页
SolutionsChapter 3.docx_第5页
第5页 / 共11页
点击查看更多>>
下载资源
资源描述

SolutionsChapter 3.docx

《SolutionsChapter 3.docx》由会员分享,可在线阅读,更多相关《SolutionsChapter 3.docx(11页珍藏版)》请在冰豆网上搜索。

SolutionsChapter 3.docx

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

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 党团工作 > 入党转正申请

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1