《Python编程-从入门到实践》课后习题(3)

3-1

friends = ['Ann', 'Willy', 'Bob', 'John']
for friend in friends:
    print(friend)

3-2

friends = ['Ann', 'Willy', 'Bob', 'John']
for friend in friends:
    print(friend + '! Good morning!')

3-3

vehicles = ['motorcycle', 'helicopter', 'boat', 'car']
for vehicle in vehicles:
    print('I would like to own a ' + vehicle)

3-4

guests= ['Ann', 'Willy', 'Bob', 'John', 'Steven']
for guest in guests:
    print('Would you like to have a dinner with me? ' + guest + '.')

3-5

guests= ['Ann', 'Willy', 'Bob', 'John', 'Steven']
for guest in guests:
    print('Would you like to have a dinner with me? ' + guest + '.')
print(guests[4] + " wouldn't.")
guests[4] = 'People'
for guest in guests:
    print('Would you like to have a dinner with me? ' + guest + '.')

3-6

guests= ['Ann', 'Willy', 'Bob', 'John', 'Steven']
for guest in guests:
    print('Would you like to have a dinner with me? ' + guest + '.')
print(guests[4] + " wouldn't.")
guests[4] = 'People'
for guest in guests:
    print('Would you like to have a dinner with me? ' + guest + '.')
print('There is a bigger table for more people.')
guests.insert(0, 'Joey')
guests.insert(3, 'Matt')
guests.append('Kim')
for guest in guests:
    print('Would you like to have a dinner with me? ' + guest + '.')

3-7

guests= ['Ann', 'Willy', 'Bob', 'John', 'Steven']
for guest in guests:
    print('Would you like to have a dinner with me? ' + guest + '.')
print(guests[4] + " wouldn't.")
guests[4] = 'People'
for guest in guests:
    print('Would you like to have a dinner with me? ' + guest + '.')
print('There is a bigger table for more people.')
guests.insert(0, 'Joey')
guests.insert(3, 'Matt')
guests.append('Kim')
for guest in guests:
    print('Would you like to have a dinner with me? ' + guest + '.')
print("The table won't come. Only two people can stay here.")
while len(guests) > 2:
    guest = guests.pop()
    print('Sorry! You have to get out here. ' + guest)
for guest in guests:
    print('You are still invited,' + guest)
for i in range(2):
    del guests[i-1]
for guest in guests:
    print(guest)

3-8

citys = ['London', 'York', 'Tokyo', 'California', 'Mexico']
for city in citys:
    print(city)
for city in sorted(citys):
    print(city)
for city in citys:
    print(city)
citys.reverse()
for city in citys:
    print(city)
citys.reverse()
for city in citys:
    print(city)
citys.sort()
for city in citys:
    print(city)
citys.sort(reverse=True)
for city in citys:
    print(city)

猜你喜欢

转载自blog.csdn.net/wanghj47/article/details/79559528
今日推荐