python从入门到实践第三章习题(高级编程技术 week2-1)

高级编程技术 (week2-1)

3-1

names = ['Mike', 'John', 'Honda']
for name in names:
    print(name)

3-2 问候语

names = ['Mike', 'John', 'Honda']
for name in names:
    print(name, ' , how do you do?')

3-3 自己的列表

vicheles = ['by motorcycle','by walk','on foot', 'by taxi']
for vichele in vicheles:
    print('I would like to go to school ',vichele)

3-4 、 3-5

这两题主要使用的是列表的基本使用,列表的元素删除,还有列表元素的修改。

names = ['Mike', 'John', 'Honda']
for name in names:
    print(name.title(), "!, would you like to have dinner with me?")

busy_name = names.pop(0)

print(name.title(), " is too busy to have dinner with me. It's so pity.")

for name in names:
    print(name.title(), "!, would you like to have dinner with me?")

3-6 3-7

这两道题主要练习的是python列表中的insert(), append()函数的使用。

# 接上一题的程序

print("I find a big table!")
names.insert(0, "Lily")
names.insert(2, "Kuly")
names.append("Xiao Ming")

for name in names:
    print(name.title(), "!, would you like to have dinner with me?")

print("But the new table can't get on time. Now I only can invite two friends to my dinner.")

while len(names) > 2:
    delete_name = names.pop()
    print(delete_name.title(), ", I am sorry to tell you that I can't invite you to my dinner.")

for name in names:
    print(name.title(), ", you still can have dinner with me!")

del names[0]
del names[0]

print(names)

3-11 引发错误

这样的程序能够引发错误

test = [1,2,3,4]
print(test[4])

运行截图如图所示:

猜你喜欢

转载自blog.csdn.net/wyfwyf12321/article/details/79523672
今日推荐