第二周第一次作业

3-1 姓名:

names=['chen','li','wang','sun']
for x in names:
    print (x.title())

Output:

/Users/macbook/PycharmProjects/py1/venv/bin/python /Users/macbook/PycharmProjects/py1/text.py
Chen
Li
Wang
Sun

Process finished with exit code 0

3-2 问候语:

names=['chen','li','wang','sun']
for x in names:
    print (x.title(),', good morning!')

Output:

/Users/macbook/PycharmProjects/py1/venv/bin/python /Users/macbook/PycharmProjects/py1/text.py
Chen , good morning!
Li , good morning!
Wang , good morning!
Sun , good morning!

Process finished with exit code 0

3-3 自己的列表:

com=['motocycle','high-speed train','bike']
print('I would like to own a Honda'+com[0])
print('I would like to go home by'+com[1])
print('I would like to go to school by '+com[2])

Output:

/Users/macbook/PycharmProjects/py1/venv/bin/python /Users/macbook/PycharmProjects/py1/text.py
I would like to own a Hondamotocycle
I would like to go home byhigh-speed train
I would like to go to school by bike

Process finished with exit code 0

3-4 嘉宾名单:

names=['chen','li','wang','sun']
for x in names:
    print (x.title(),',would you like to have dinner with me?')

Output:

/Users/macbook/PycharmProjects/py1/venv/bin/python /Users/macbook/PycharmProjects/py1/text.py
Chen ,would you like to have dinner with me?
Li ,would you like to have dinner with me?
Wang ,would you like to have dinner with me?
Sun ,would you like to have dinner with me?

Process finished with exit code 0

3-5 修改嘉宾名单:

names=['chen','li','wang','sun']
for x in names:
    print (x.title(),',would you like to have dinner with me?')
print()
print("It's a pity that",names[0].title(),"can't come!");
names[0]='zheng'
print()
for x in names:
    print (x.title(),',would you like to have dinner with me?')

Output:

/Users/macbook/PycharmProjects/py1/venv/bin/python /Users/macbook/PycharmProjects/py1/text.py
Chen ,would you like to have dinner with me?
Li ,would you like to have dinner with me?
Wang ,would you like to have dinner with me?
Sun ,would you like to have dinner with me?

It's a pity that Chen can't come!

Zheng ,would you like to have dinner with me?
Li ,would you like to have dinner with me?
Wang ,would you like to have dinner with me?
Sun ,would you like to have dinner with me?

Process finished with exit code 0

3-6 添加嘉宾:

names=['chen','li','wang','sun']
for x in names:
    print (x.title(),',would you like to have dinner with me?')
print()
print('I have found a bigger dinner-table')
print()
names.insert(0,'lin')
names.insert(3,'hu')
names.append('zeng')
for x in names:
    print (x.title(),',would you like to have dinner with me?')

Output:

/Users/macbook/PycharmProjects/py1/venv/bin/python /Users/macbook/PycharmProjects/py1/text.py
Chen ,would you like to have dinner with me?
Li ,would you like to have dinner with me?
Wang ,would you like to have dinner with me?
Sun ,would you like to have dinner with me?

I have found a bigger dinner-table

Lin ,would you like to have dinner with me?
Chen ,would you like to have dinner with me?
Li ,would you like to have dinner with me?
Hu ,would you like to have dinner with me?
Wang ,would you like to have dinner with me?
Sun ,would you like to have dinner with me?
Zeng ,would you like to have dinner with me?

Process finished with exit code 0

3-7 缩减名单:

names=['chen','li','wang','sun']
for x in names:
    print (x.title(),',would you like to have dinner with me?')
print()
print('I have found a bigger dinner-table')
print()
names.insert(0,'lin')
names.insert(3,'hu')
names.append('zeng')
for x in names:
    print (x.title(),',would you like to have dinner with me?')
print()
print("It's a pity that I can only invite two people to have dinner")
print()
while len(names)>2:
    person=names.pop()
    print('My dear friend',person,",I'm sorry that I can't invite you for dinner!")
for x in names:
    print(x.title(), ',have dinner with me!')
del names[0]
del names[0]
print(names)

Output:

/Users/macbook/PycharmProjects/py1/venv/bin/python /Users/macbook/PycharmProjects/py1/text.py
Chen ,would you like to have dinner with me?
Li ,would you like to have dinner with me?
Wang ,would you like to have dinner with me?
Sun ,would you like to have dinner with me?

I have found a bigger dinner-table

Lin ,would you like to have dinner with me?
Chen ,would you like to have dinner with me?
Li ,would you like to have dinner with me?
Hu ,would you like to have dinner with me?
Wang ,would you like to have dinner with me?
Sun ,would you like to have dinner with me?
Zeng ,would you like to have dinner with me?

It's a pity that I can only invite two people to have dinner

My dear friend zeng ,I'm sorry that I can't invite you for dinner!
My dear friend sun ,I'm sorry that I can't invite you for dinner!
My dear friend wang ,I'm sorry that I can't invite you for dinner!
My dear friend hu ,I'm sorry that I can't invite you for dinner!
My dear friend li ,I'm sorry that I can't invite you for dinner!
Lin ,have dinner with me!
Chen ,have dinner with me!
[]

Process finished with exit code 0

3-8 放眼世界:

place=['paris','london','hongKong','tokyo','berlin']
print('Original:',place)
print('Sorted:',sorted(place))
print('Again:',place)
print('reverse_sorted:',sorted(place,reverse=True))
print('Again:',place)
place.reverse()
print('reverse',place)
place.reverse()
print('reverse_again:',place)
place.sort()
print('sort:',place)
place.sort(reverse=True)
print('reverse_sort:',place)

Output:

/Users/macbook/PycharmProjects/py1/venv/bin/python /Users/macbook/PycharmProjects/py1/text.py
Original: ['paris', 'london', 'hongKong', 'tokyo', 'berlin']
Sorted: ['berlin', 'hongKong', 'london', 'paris', 'tokyo']
Again: ['paris', 'london', 'hongKong', 'tokyo', 'berlin']
reverse_sorted: ['tokyo', 'paris', 'london', 'hongKong', 'berlin']
Again: ['paris', 'london', 'hongKong', 'tokyo', 'berlin']
reverse ['berlin', 'tokyo', 'hongKong', 'london', 'paris']
reverse_again: ['paris', 'london', 'hongKong', 'tokyo', 'berlin']
sort: ['berlin', 'hongKong', 'london', 'paris', 'tokyo']
reverse_sort: ['tokyo', 'paris', 'london', 'hongKong', 'berlin']

Process finished with exit code 0

3-9晚餐嘉宾:

names=['chen','li','wang','sun']
for x in names:
    print (x.title(),',would you like to have dinner with me?')
print()
print('I have invited',len(names),'guests to have dinner together')

Output:

/Users/macbook/PycharmProjects/py1/venv/bin/python /Users/macbook/PycharmProjects/py1/text.py
Chen ,would you like to have dinner with me?
Li ,would you like to have dinner with me?
Wang ,would you like to have dinner with me?
Sun ,would you like to have dinner with me?

I have invited 4 guests to have dinner together

Process finished with exit code 0

3-11 有意引发错误

list1=[1,2,3,4,5]
print(list1[5])

Output

/Users/macbook/PycharmProjects/py1/venv/bin/python /Users/macbook/PycharmProjects/py1/text.py
Traceback (most recent call last):
  File "/Users/macbook/PycharmProjects/py1/text.py", line 2, in <module>
    print(list1[5])
IndexError: list index out of range

Process finished with exit code 1










  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值