Python教材第三章部分习题

教材:《Python编程 从入门到实践》

3-4:嘉宾名单

# 3-4 Guests!
guests = [
    'Jared "PiG" Krensel', 'Evan "Winter" Ballnik', 'Simon "Lowko" Heijnen',
    'Austin "Neuro" Filsinger'
]

for guest in guests:
    print("Dear " + guest + ", do I own the honor to have dinner with you?")

输出:

Dear Jared "PiG" Krensel, do I own the honor to have dinner with you?
Dear Evan "Winter" Ballnik, do I own the honor to have dinner with you?
Dear Simon "Lowko" Heijnen, do I own the honor to have dinner with you?
Dear Austin "Neuro" Filsinger, do I own the honor to have dinner with you?

3-6:添加嘉宾

# 3-6 More Guests!
guests = [
    'Jared "PiG" Krensel', 'Evan "Winter" Ballnik', 'Simon "Lowko" Heijnen',
    'Austin "Neuro" Filsinger'
]

for guest in guests:
    print("Dear " + guest + ", do I own the honor to have dinner with you?")

print("Oh I've just found a larger table, let's invite more people to dinner!")

guests.insert(0, 'Dan "Artosis" Stemkoski')
guests.insert(3, 'Nicolas "Tasteless" Plott')
guests.append('Yoan "ToD" Merlo')

print("\nResending invites...")
for guest in guests:
    print("Dear " + guest + ", do I own the honor to have dinner with you?")

输出:

Dear Jared "PiG" Krensel, do I own the honor to have dinner with you?
Dear Evan "Winter" Ballnik, do I own the honor to have dinner with you?
Dear Simon "Lowko" Heijnen, do I own the honor to have dinner with you?
Dear Austin "Neuro" Filsinger, do I own the honor to have dinner with you?
Oh I've just found a larger table, let's invite more people to dinner!

Resending invites...
Dear Dan "Artosis" Stemkoski, do I own the honor to have dinner with you?
Dear Jared "PiG" Krensel, do I own the honor to have dinner with you?
Dear Evan "Winter" Ballnik, do I own the honor to have dinner with you?
Dear Nicolas "Tasteless" Plott, do I own the honor to have dinner with you?
Dear Simon "Lowko" Heijnen, do I own the honor to have dinner with you?
Dear Austin "Neuro" Filsinger, do I own the honor to have dinner with you?
Dear Yoan "ToD" Merlo, do I own the honor to have dinner with you?

3-11:有意引发错误

# 3-11 I am so asking for it
guests = [
    'Jared "PiG" Krensel', 'Evan "Winter" Ballnik', 'Simon "Lowko" Heijnen',
    'Austin "Neuro" Filsinger'
]

print("There are " + str(len(guests)) + " guests.")

print(guests[4])

输出:

There are 4 guests.
Traceback (most recent call last):
  File "ch2.py", line 9, in 
    print(guests[4])
IndexError: list index out of range



你可能感兴趣的:(Python课程笔记)