1.•利用 map() 函数,把用户输入的不规范的英文名字,变为首字母大写,其他小
写的规范名字。输入: ['adam', 'LISA', 'barT'] ,输出: ['Adam', 'Lisa',
'Bart'] 。
In [4]: def fun(x):
return x.title()
...:
In [5]: map(fun,['adam', 'LISA', 'barT'])
2.•Python 提供的 sum() 函数可以接受一个 list 并求和,请编写一个 prod()
函数,可以接受一个 list 并利用 reduce() 求积。
In [7]: def prod(x,y):
return x*y
...:
In [8]: reduce(prod,[1,2,3,4])
3.
#!/usr/bin/env python
# coding:utf-8
_author_ = "tom"
'''
@author:houruiyun
@file:2.py
@contact:674211605.qq.com
@time:6/25/172:45 AM
@desc:
'''
print '''
Welcome to shoping system
可选择操作
b:购买
q:退出
'''
goods = [
('iphone',5800),
('book',100),
('bike',200)
]
buy = []
for k,v in enumerate(goods):
print k,v
money = int(raw_input('请输入账户余额:'))
while True:
oper = raw_input('plsase input your choice:')
if oper == 'b':
num = input('请选择商品编号:')
price = goods[num][1]
if money < price:
print '账户余额不足'
else:
money = money - price
print '%s to your shoping' % goods[num][0]
buy.append(goods[num][0])
print '''
购买商品:%s
消费金额:%s
账户余额:%s
''' % (buy, price, money)
else:
exit()
写的规范名字。输入: ['adam', 'LISA', 'barT'] ,输出: ['Adam', 'Lisa',
'Bart'] 。
In [4]: def fun(x):
return x.title()
...:
In [5]: map(fun,['adam', 'LISA', 'barT'])
Out[5]: ['Adam', 'Lisa', 'Bart']
2.•Python 提供的 sum() 函数可以接受一个 list 并求和,请编写一个 prod()
函数,可以接受一个 list 并利用 reduce() 求积。
In [7]: def prod(x,y):
return x*y
...:
In [8]: reduce(prod,[1,2,3,4])
Out[8]: 24
3.
#!/usr/bin/env python
# coding:utf-8
_author_ = "tom"
'''
@author:houruiyun
@file:2.py
@contact:674211605.qq.com
@time:6/25/172:45 AM
@desc:
'''
print '''
Welcome to shoping system
可选择操作
b:购买
q:退出
'''
goods = [
('iphone',5800),
('book',100),
('bike',200)
]
buy = []
for k,v in enumerate(goods):
print k,v
money = int(raw_input('请输入账户余额:'))
while True:
oper = raw_input('plsase input your choice:')
if oper == 'b':
num = input('请选择商品编号:')
price = goods[num][1]
if money < price:
print '账户余额不足'
else:
money = money - price
print '%s to your shoping' % goods[num][0]
buy.append(goods[num][0])
print '''
购买商品:%s
消费金额:%s
账户余额:%s
''' % (buy, price, money)
else:
exit()
4.登陆系统