习题3-7:
invite_list=['丁丁','东东','芳芳']
print(invite_list[0]+'无法赴约!')
invite_list[0]='明明'
invite_list.insert(0,'大鹏')
invite_list.insert(2,'小花')
invite_list.append('比比')
for name in invite_list:
print('欢迎参加宴会'+name+'!')
print('我们只能邀请两位嘉宾出席宴会!')
n=len(invite_list)
for n in range(1,n-1):
name=invite_list.pop()
print(name+'不能出席!')
for name in invite_list:
print('欢迎参加宴会' + name + '!')
习题4-13 tuple元组不可修改
foods=('西瓜','苹果','香蕉','菠萝')
try:
foods[0]='哈密瓜'
except Exception as msg:
print(msg)
for food in foods :
print(food)
习题6-6
favorite_language={'丁丁':'c','小明':'c++','小花':'python','东东':'java'}
names=['丁丁','小明','小王','小李']
for name in names:
if name in favorite_language.keys():
print(name+',谢谢参与')
else:
print(name+',烦请参加')
习题6-10
favorite_no={'小明':['1','3'],'小花':['4','5'],'小丽':['6','7']}
for m,n in favorite_no.items():
print(m+'最喜欢的数字是')
for v in n:
print(v)
print('这两个数字')
习题7-9
sandwich_orders=['沙拉三明治','火腿三明治','鸡蛋三明治','金枪鱼三明治','蔬菜三明治']
finished_sandwich=[]
while True:
if len(sandwich_orders)!=0:
for sd1 in sandwich_orders:
if sd1=='火腿三明治':
sandwich_orders.remove(sd1)
else:
fs=sandwich_orders.pop()
print(fs)
finished_sandwich.append(fs)
print('已完成'+fs)
else:
print('所有完成的三明治如下:')
for sd in finished_sandwich:
print(sd)
break
习题7-10
name='你的名字'
place='你想去的地方'
active=True
place_wanttogo={}
while active:
names=input(name)
places=input(place)
place_wanttogo[names]=places
print(names+places)
msg=input('想退出吗')
if msg=='YES':
active = False
elif msg=='no':
pass
习题8-11
magicians=['东东','小明','小花','大鹏']
new_one=[]
def make_great(magicians,new_one):
while magicians:
magician=magicians.pop()
name='the Great'+magician
new_one.append(name)
def show_magician():
for magician in new_one:
print(magician)
make_great(magicians[:],new_one)
show_magician()
print(magicians)
习题8-13
def my_profile(first, last, **other_info):
my_info = {}
my_info['first_name'] = first
my_info['last_name'] = last
for k, v in other_info.items():
my_info[k] = v
return my_info
print(my_profile('ma', 'yun', company = 'taobao', age = '60'))
版权声明:本文为Aliciaz原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。