高级编程技术 Python 第四周作业

第七章: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的使用场合大大拓宽了。此时已经可以写有一定规模的程序了。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值