第七章:input和while
#7-2
a = input('How many people are there to have dinner? Please input: ')
a = int(a)
if (a > 8):
print('There is no enough seats left.')
else:
print('There is enough seats. Welcome to have dinner.')
#7-3
a = input('Please input an integer: ')
a = int(a)
if (a%10 == 0):
print('a = 10k')
else:
print('a != 10k')
#7-7
i = 0
while (True):
i = i+1
#7-10
responses = {}
have_next = True
while (have_next):
name = input('Please input your name: ')
response = input('Where would you like to go in summer holiday? ')
responses[name] = response
repeat = input('Is there the next one? (y/n) ')
if (repeat.lower() == 'n'):
have_next = False
print('\n')
print('Results:')
for name, response in responses.items():
print(name+'\'d like to go to '+response+'.')
第八章:函数
#8-1
def display_message():
print('Hello world!')
display_message()
#8-5
def discribe_city(city, country='united states'):
print(city.title()+' is in '+country.title()+'.')
discribe_city('new york', 'united states')
discribe_city('san francisco')
discribe_city('toronto', 'canada')
#8-8
def make_music(musician, name):
temp = {'musician': musician, 'name': name}
return temp
def make_movement(music, name):
music['movement '+str(count)] = name
album = {}
have_next = 'n'
while (have_next.lower() != 'y'):
musician = input('Please input musician: ')
name = input('Please input music name: ')
album[name] = make_music(musician, name)
have_next_2 = 'n'
count = 1
while (have_next_2.lower() != 'y'):
movement = input('Please input movement name: ')
make_movement(album[name], movement)
count = count+1
have_next_2 = input('Is the movement the last movement? (y/n) ')
have_next = input('Is the music the last work? (y/n) ')
for musician, music in album.items():
print('\n')
for item, value in music.items():
print(item.title()+': '+value.title())
掌握了函数的使用方法之后,Python的使用场合大大拓宽了。此时已经可以写有一定规模的程序了。