最近学习了Python的一些基础数据结构,这里通过一个小程序做一个小的总结,自己比较偏好字典,故程序中主要使用字典数据结构。
1. 通过用户名和密码认证才能登陆系统,注册后需登陆。
2. 认证通过后,系统会给出几个旅行计划,计划中有可以选择的目的地及旅行天数。
3. 确定购买结束后,结算总消费金额并退出。
期望用到的python知识点有:
1.通用序列的操作和列表的方法。
2.字典的方法。
3.流程控制。
import time #create authentification list authlist={‘zhang01‘:‘1234qwer‘,‘lee10‘:‘1qaz‘,‘wang33‘:‘567890‘} user=raw_input(‘please enter your username: ‘) passwd=raw_input(‘please enter your password: ‘) #for key,value in authlist.items(): # if user== key and passwd==value: # print ‘login successful!‘ # elif user!=key: # print ‘no such account!‘ # elif passwd!=value: # print ‘invalid passwd!‘ # break #login verification #尝试用循环遍历字典方法,但未成功。 while True: if user in authlist: if passwd==authlist.get(user): print ‘login successful!‘ break else: print ‘invalid password!‘ passwd=raw_input(‘please enter your password: ‘) continue else: print ‘no such account,please register!‘ newuser= raw_input(‘please enter a username: ‘) if newuser not in authlist: print ‘username is ok!‘ newpasswd=raw_input(‘please enter a password: ‘) authlist.setdefault(newuser,newpasswd) print ‘register successful!‘ break else: print ‘username is already existed!‘ #travel plans offer c1={‘1‘:100,‘3‘:300,‘5‘:500,‘14‘:1000} c2={‘1‘:300,‘3‘:500,‘5‘:700,‘14‘:1300} c3={‘1‘:500,‘3‘:700,‘5‘:900,‘14‘:1500} c4={‘1‘:700,‘3‘:900,‘5‘:1000,‘14‘:1700} costs=dict(domestic=c1, japan=c2,europe=c3,usa=c4) print ‘the costs for different destinations for 1,3,5,14 days: \n‘, costs print ‘\n‘ time.sleep(3) shlist={} index=0 a=1 cost=0 d=0 while a: dest=raw_input(‘please choose your destionation: ‘) days=raw_input(‘please choose your days of staying: ‘) cost=costs[dest][days] print ‘you want to spend %s days in %s,and the cost is $%s ‘% (days,dest,cost) shlist[index]=(dest,days,cost) index +=1 d +=cost con=raw_input(‘are you done shopping? yes(Y) or no(N): ‘) if con==‘Y‘:a=0 else:a=1 print ‘your shopping cart has %d item:%s, total cost is $%d‘ %(index,shlist.values(),d) key=raw_input( ‘do you want to check out(C) or delete some item in shopping list(D)?‘) if key == ‘C‘: print ‘your current shopping list is: ‘ print shlist print ‘please pay $%d‘ %d if key == ‘D‘: print shlist k=raw_input( ‘please enter the item number you want to delete in your shopping list: ‘) shlist.pop(int(k)) if shlist=={}: print ‘your current shopping list is empty‘ else: print ‘your current shopping list is: ‘ print shlist
参照 http://467754239.blog.51cto.com/4878013/1572203 (全部用列表实现)还有以下几点可以改进:
- autenlist和travelplans可以单独存放于txt文件,用open打开,readlines读取。
- 通过index返回list中某元素位置,再以此访问与其对应的list中该位置的值。
- 可以增加一个预算和余额功能。
时间: 2024-10-11 04:51:20