本文为大家分享了购物商城小程序,供大家参考,具体内容如下
软件版本:python3.x
功能:实现简单购物商城
1.允许用户选择购买多少件
2.允许多用户登录,下一次登录后,继续按上次的余额继续购买
3. 允许用户查看之前的购买记录(显示购买时间)
4. 商品列表分级展示
操作:
1.默认用户,pan,li,密码为123
2.登录后需正确输入用户名和密码
3.按提示选择充值的金额
4.选择购买的商品,按q退出,按c查看易购买记录,按s查看当前已购买商品
注:file_lock.txt,username_file.txt需事先创建
代码如下:
#!/usr/bin/env python #coding:utf-8 #Auto:Panwenbin #function: import shelve import time shop_list={ #定义商品清单 '数码电器':{'电脑':'8000','照相机':'10000','手机':'3800','打印机':'3600'}, '服装百货':{'牛仔裤':'288','夹克':'300','王老吉':'6','方便面':'4'}, '汽车':{'特斯拉':'999999','宝马X5':'550000','帕沙特':'250000','奇瑞':'100000'}, '化妆品':{'欧莱雅':'888','欧诗漫':'666','韩束':'388','百雀羚':'259'}, } count=0#定义一个计数器 jiage=[] shop_list1={} shop_name=list(shop_list.keys()) #将商品第一层清单数据类型转换为列表 lock_file=open('file_lock.txt','r+') user_pass=open('username_file.txt','r+') count=0 def show_list():#打印序列号与商品分类清单 for i,j in enumerate(shop_list): print('%d:%s'%(i,j)) def show_menu(user_choise):#打印序列号与商品第二层清单 user_choise=int(user_choise) a=shop_name[user_choise] for index,key in enumerate(shop_list[a]): #打印序列号与商品第二层清单 print('%d:%s %s'%(index,key,shop_list[a][key])) def show_menu2(user_choise,user_choise2):#调用用户第一次选择和第二次选择,进入对应的购物列表 user_choise=int(user_choise) if user_choise2.isdigit() and int(user_choise2) < len(shop_list[shop_name[int(user_choise)]]):#判断用户输入的是否为数字,并小于商品序列号 user_choise2=int(user_choise2) # if user_choise2==0: jiage.clear() for v,k in enumerate(shop_list[shop_name[user_choise]]): #获取用户进入第二层商品的清单 jiage.append(shop_list[shop_name[user_choise]][k])#将获取的商品清单存入列表中 jiage2=(int(jiage[user_choise2])) global jiage2 def quit_time():#查询完成后退出 for i in [3,2,1]: print('\033[32;1m查询完毕,正在返回主菜单.....\033[1m',i) time.sleep(1) def start(): while True: if user_choise.isdigit() and int(user_choise) <len(shop_list):#判断用户输入的是否为数字,并小于商品序列号 show_menu(user_choise)#调用show_menu函数, break else: print('\033[31;1m无效选项,请重新输入\033[1m')#用户重新输入 break ######################################################################################################################## while count <3:#只要重试不超过3次就不断循环 print('\033[31;1m默认用户名密码为:pan,123 li,123\033[1m') username=input('\033[34;1m请输入您的用户名:\033[1m') for i in lock_file.readlines(): #判断用户名是否在锁文件中 i=i.split() if username in i[0]: print('\033[31;1m对不起 %s 已锁定\033[1m'%username) exit() match = False for j in user_pass.readlines(): user,password = j.strip('\n').split() #去掉每行多余的\n并把这一行按空格分成两列,分别赋值为user,passwd两个变量 if username == user: #判断输入的用户是否存在 passwd=input ('\033[30;1m请输入密码:') if password == passwd: match = True break elif password != passwd:#在用户名正确的前提下,判断输入的密码是否正确 for i in range(2): passwd=input ('\033[31;1m密码错误,请重新输入密码:\033[1m') if password == passwd: match =True break else: print('\033[31;1m密码和用户名不匹配,尝试超过三次,用户被锁定\033[1m') lock_file.write('%s \n'%username) lock_file.close() user_pass.close() exit() if username != user: print('\033[31;1m您输入用户名不存在,程序已退出\033[1m') exit() elif match==True: break ######################################################################################################################## f=shelve.open('user.db','c+') try: if f[user] >0: pay=f[user] chong_zhi=input('\033[33;1m您的当前余额为%d,是否充值?充值请输入您要充值的金额,任意键进入下一步,退出程序请按q:\033[1m'%f[user]) if chong_zhi.isdigit() and int(chong_zhi) > 0: pay=int(chong_zhi)+int(pay) print('\033[35;1m充值后金额为\033[1m',pay) except KeyError: chong_zhi=input('\033[36;1m您的当前余额为0,是否充值?充值请输入您要充值的金额,退出程序请按q:\033[1m') while True: if chong_zhi.isdigit() and int(chong_zhi) > 0: pay=int(chong_zhi) print('\033[33;1m充值后金额为\033[1m',pay) break elif chong_zhi=='q': print('\033[36;1m程序正在退出\033[1m') exit() else: pass # pay=input('您的当前余额为0,是否充值?充值请输入您要充值的金额,退出程序请按q:') ######################################################################################################################## while True: show_list()#调用show_list函数,打印商品分类清单 user_choise=input('\033[32;1m选择购买商品的类型:\033[1m') #获取用户选择商品的分类 start()#调用start函数 user_choise2=input('\033[36;1m选择购买商品的类型。按q退出,按c查看易购买记录,按s查看当前已购买商品,任意键返回上一级菜单,:\033[0m')#获取用户选择的商品 # user_num=input('\033[35;1m请选择需要购买的件数,默认为1:\033[0m')#获取用户选择商品的数量 if user_choise2.isdigit() and int(user_choise2) < len(shop_list[shop_name[int(user_choise)]]):#判断用户输入的是否为数字,并小于商品序列号 user_num=input('\033[35;1m请选择需要购买的件数,默认为1:\033[1m')#获取用户选择商品的数量 show_menu2(user_choise,user_choise2)#调用show_menu2函数,获取用户选择商品的种类和数量,可用金额是否满足 if user_num.strip() =='':#如果用户输入为空,默认为1 user_num=int(1) elif user_num.isdigit and int(user_num) > 1:#如果用户输入是数字切大于1,获得用户输入中 user_num=int(user_num) else: user_num=int(1)#其余情况下默认为一 pay=int(pay) if pay > jiage2*user_num:#判断用户选择商品的价格*数量是否可以支付 pay=pay-jiage2*user_num choise=list(shop_list[shop_name[int(user_choise)]].keys())#得到用户进入第二层商品列表 count+=1 #计数器值加一 goumai_jilu=shelve.open('goumai_jilu.txt','a+') #调用shelve打开一个数据文件 goumai_jilu[str(count)]=choise[int(user_choise2)],jiage2,user_num,time.ctime()#向数据文件中插入用户选择的商品,个数和购买时间 goumai_jilu.close()#关闭数据文件 a=choise[int(user_choise2)]#得到用户选择的商品,choise为用户选择第二层商品列表 if a in shop_list1: #如果用户选择的商品已购买 shop_list1[a][0]=int(shop_list1[a][0])+user_num #将购买数加一 shop_list1[a][1]=int(shop_list1[a][1])+int(shop_list1[a][1]*user_num) #将总额加 else: shop_list1[a]=list([1,jiage2]) print('\033[32;1m您的余额为:\033[1m',pay) else: print('\033[32;1m您的余额不足,请重新选择:\033[0m') elif user_choise2=='b':#返回商品分类清单 continue elif user_choise2=='q':#退出程序 f=shelve.open('user.db','a+') f[user]=pay f.close() exit() elif user_choise2=='c': goumai_jilu=shelve.open('goumai_jilu.txt','a+') print('\033[32;1m您的当前购买记录为:\033[1m') for i in goumai_jilu.items(): #获取购买记录中的数据 print('{} {}'.format(i[0],goumai_jilu[i[0]]))#打印购买记录 print('\033[32;1m################################################\033[0m') quit_time() elif user_choise2=='s': print('\033[32;1mp_name num total_price\033[1m') print('\033[32;1m%-10s%-10s%-10s\033[1m'%(a,shop_list1[a][0],shop_list1[a][1]))#打印当前购买的商品,个数以及总额 print('\033[32;1m################################################\033[0m') quit_time() else: print('\033[41;33m无效选项,请重新选择:\033[0m') continue
以上就是本文的全部内容,希望对大家学习Python程序设计有所帮助。
更多python实现简单购物商城相关文章请关注PHP中文网!

Python and C each have their own advantages, and the choice should be based on project requirements. 1) Python is suitable for rapid development and data processing due to its concise syntax and dynamic typing. 2)C is suitable for high performance and system programming due to its static typing and manual memory management.

Choosing Python or C depends on project requirements: 1) If you need rapid development, data processing and prototype design, choose Python; 2) If you need high performance, low latency and close hardware control, choose C.

By investing 2 hours of Python learning every day, you can effectively improve your programming skills. 1. Learn new knowledge: read documents or watch tutorials. 2. Practice: Write code and complete exercises. 3. Review: Consolidate the content you have learned. 4. Project practice: Apply what you have learned in actual projects. Such a structured learning plan can help you systematically master Python and achieve career goals.

Methods to learn Python efficiently within two hours include: 1. Review the basic knowledge and ensure that you are familiar with Python installation and basic syntax; 2. Understand the core concepts of Python, such as variables, lists, functions, etc.; 3. Master basic and advanced usage by using examples; 4. Learn common errors and debugging techniques; 5. Apply performance optimization and best practices, such as using list comprehensions and following the PEP8 style guide.

Python is suitable for beginners and data science, and C is suitable for system programming and game development. 1. Python is simple and easy to use, suitable for data science and web development. 2.C provides high performance and control, suitable for game development and system programming. The choice should be based on project needs and personal interests.

Python is more suitable for data science and rapid development, while C is more suitable for high performance and system programming. 1. Python syntax is concise and easy to learn, suitable for data processing and scientific computing. 2.C has complex syntax but excellent performance and is often used in game development and system programming.

It is feasible to invest two hours a day to learn Python. 1. Learn new knowledge: Learn new concepts in one hour, such as lists and dictionaries. 2. Practice and exercises: Use one hour to perform programming exercises, such as writing small programs. Through reasonable planning and perseverance, you can master the core concepts of Python in a short time.

Python is easier to learn and use, while C is more powerful but complex. 1. Python syntax is concise and suitable for beginners. Dynamic typing and automatic memory management make it easy to use, but may cause runtime errors. 2.C provides low-level control and advanced features, suitable for high-performance applications, but has a high learning threshold and requires manual memory and type safety management.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SublimeText3 Mac version
God-level code editing software (SublimeText3)

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

Dreamweaver CS6
Visual web development tools