import time 1). Creat : 2). Login : 3). Delete : 4). View : 登陆时判断本次登陆与上次登陆时间的差,如果time<4h,显示,你已经登陆在4小时前。 源代码:
#!/usr/bin/env python # coding:utf-8 __author__ = "lvah" import time ''' @author:fairy @file:8.py @time:7/10/1711:11 PM @DESC; ''' hour = 4 * 60 * 60 USER = {} def creat(): name1 = raw_input('new username:') if name1 not in USER.keys(): pass1 = raw_input('new passwprd:') USER[name1] = [pass1,0] else: print 'the username is existed,please give me a new name!' creat() def login(): if USER.keys() == 0: print 'no users exist,please create user' else: user_in = raw_input('username:') pass_in = raw_input('password:') d1 = time.time() if USER[user_in][1] == 0: if user_in in USER.keys() and pass_in == USER[user_in][0]: USER[user_in][1] = d1 print 'welcome to user system!' else: print 'error username or password!' else: if user_in in USER.keys() and pass_in == USER[user_in][0]: if d1 - USER[user_in][1] >= hour: print 'wlecome to user system!' else: d2 = (hour - (d1 - USER[user_in][1])) / 60 print 'you have been login in within 4 hours,please login after %d minutes' % d2 else: print 'error username or password!' def delete(): user1 = raw_input('username:') USER.pop(user1) print 'delete user successful' def view(): print USER.keys() def main(): cho = 'Enter your choice:' print ''' you can input: creare(c) login(l) delete(d) view(v) ''' while True: pro = raw_input(cho).lower() if pro == 'c': creat() elif pro == 'l': login() elif pro == 'd': delete() elif pro == 'v': view() else: print 'error:you can input c l d v' main()测试:
1)启动程序后,让用户输入公子,然后打印商品列表; 2)允许用户根据商品编号购买商品; 3)用户选择商品后,检测余额是否足够,够就直接扣款,不够就提醒; 4)可随时退出,退出时打印已购买的商品和余额; your salary: 1 ipthon 5800 2 book 100 3 bike 200 >>>:1 added [ipthon] to your shopping >>>:3 added [bike] to your shopping >>>:q
have bought below:
#!/usr/bin/env python # coding:utf-8 _author_ = "lvah" ''' @author:fairy @file:homework_shopping.py @contact:xiaojun0118@foxmail.com @time:7/9/1711:17 AM @DESC; ''' shopinfo = [ ['Iphon', 1000], ['book', 200], ['fentiao', 3500] ] BUY =[] def buy(): a = input('请输入工资:') print '商品列表:' for num, shanping in enumerate(shopinfo): print num, shanping[0],shanping[1] num1 = input('请输入商品编号:') if a < shopinfo[num1][1]: print '余额不足' else: a1 = a - shopinfo[num1][1] BUY.append(shopinfo[num1][0]) print '余额:%d\n%s 购买成功' % (a1,shopinfo[num1][1]) def main(): print ''' 欢迎进入购物车系统 你可以: 购买(B) 退出(Q) ''' pro = raw_input('请输入选择:').lower() if pro == 'q': print '退出系统' elif pro == 'b': buy() else: print '你可以输入B , Q' main()(1)利用map()函数,把用户输入的不规范英文名字,变为首字母大写,其他小写的规范名字。输入:[‘adam’,’LISA’,’barT’],输出:[‘Adam’,’Lisa’,’Bart’]。
#!/usr/bin/env python #coding:utf-8 __author__ = "lvah" ''' @author:fairy @file:map.py @time:7/11/179:12 PM @DESC; ''' def f(li): return li.title() print map(f,['adad','ASE','adaRR'])测试:
(2)Python提供的sum()函数可以接受一个list并求和,请编写一个prod()函数,可以接受一个list并利用reduce()求积
#!/usr/bin/env python # coding:utf-8 __author__ = "lvah" ''' @author:fairy @file:reduce.py @time:7/11/179:24 PM @DESC; ''' def f(x, y): return x * y def prod(li): return reduce(f, li) print prod([3,5,4,7])测试: