从7点到9点写的小程序(用了模块导入,python终端颜色显示,用了点局部和全局可变和不可变作用域,模块全是自定义)
程序员文章站
2022-03-06 10:25:20
要是能做的好看为啥不做的好看 在同目录下生成程序 1.程序文件 2.自制文件读取模块 3..功能模块 1.登入 2.注册 3.购物 4.余额查询(学了js模块再该) 5.4.充值(学了js模块再该) ......
未完待续的小程序
要是能做的好看为啥不做的好看
在同目录下生成程序
1.程序文件
run.py
from login import login from register import register from balance_enquiry import balance_enquiry from top_up import top_up msg_dict ={ '1':login, '0':register, '2':top_up, '3':balance_enquiry, 'q':'quit' } login_name = [none] while true: print("\033[0;31;44m\t\t杨大爷超市欢迎您\t\t\t\033[5m") print(38*'-') print("\033[0;30;42m\t\t输入0进入注册界面\t\t\t\033[5m") print("\033[0;30;42m\t\t输入1进入登入界面\t\t\t\033[5m") print("\033[0;30;42m\t\t输入2进入充值界面\t\t\t\033[5m") print("\033[0;30;42m\t\t输入3进入余额查询界面\t\t\033[5m") print("\033[0;30;42m\t\t输入q退出程序 \t\t\t\t\033[5m") print(38 * '-') user_shopping_chiose = input('请输入你要选择的功能') if user_shopping_chiose not in msg_dict: print('请好好输入') continue elif user_shopping_chiose=='q': print('再见') break msg_dict[user_shopping_chiose]()
2.自制文件读取模块
open_mode.py
def read_all(filename): with open (filename,'a+',encoding='utf8') as fr : fr.seek(0) res = fr.read() return res #文件信息格式为info_list=[name:pwd:aggregate score:the remaining points:freeze0为解冻,1为冻结:账户余额 ] #info_dict[name:[pwd:aggregate score:the remaining points:freeze0为解冻,1为冻结]] def to_info_dict(filename): info_dict = dict() with open (filename,'a+',encoding='utf8') as fr : fr.seek(0) for info in fr: info_list = info.strip().split(':') name = info_list[0] info_dict[name] = info_list[1:] return info_dict #账号:密码:总积分:剩余积分:是否为冻结0为未冻结,1位冻结:账户余额 def add_info(user_name,user_pwd): with open('用户注册信息.txt', 'a', encoding='utf8') as fw: fw.write(f'{user_name}:{user_pwd}:0:0:0:0\n') def add_purchase_history(user,goods,nums,price,discounted_price): with open('购买记录.txt', 'a', encoding='utf8') as fw: fw.write(f'{user}:{goods}:{nums}:{price}:{discounted_price}\n')
3..功能模块
1.登入
login.py
import open_mode #login_successful[0]来判断是否登入成功 login_name = [none] def login(): print("\033[0;40;46m\t\t欢迎使用登入功能\t\t\t\033[0m") info = open_mode.read_all() if info == '': print('无人注册会员请先注册') return user_dict = open_mode.to_info_dict('用户注册信息.txt') if not login_name[0]: while true: print(38*'-') print('输入q可以退出登入程序') user_name = input('请输入你的名字\n') if user_name =='q': return false if user_name not in user_dict: print('账号不存在请重新登入') continue print(38*'-') user_pwd = input('请输入你账号密码\n') if user_pwd != user_dict[user_name][0]: print('密码错误请重新登入') continue elif user_dict[user_name][3]=='1': print(f'该账号{user_name}已经被冻结不能使用') continue print('登入成功') login_name[0] = user_name return true while true: print(38*'-') print("\033[0;40;46m\t\t欢迎使用登入功能\t\t\t\033[0m") print(f'账号{login_name[0]}已经登入') chiose = input('输入1当前账号退出并退出登入程序\n' '输入2当前账号退出并重新登入程序\n' '输入q退出当前程序') print(38*'-') if chiose == '1': login_name[0] = none return false elif chiose =='2': login_name[0] = none login() return elif chiose =='q': return true else: print('请正确输入')
2.注册
register.py
import open_mode def register(): while true: print(38*'-') print("\033[0;40;46m\t\t注册功能\t\t\t\033[0m") user_name = input('请输入注册账号(退出程序输入q)\n' '(账号大于6位小于9位由纯字母组成)\n' ) if user_name=='q': return false elif len(user_name.replace(' ','')) != len(user_name): print('输入的账号有空格请重洗输入') continue elif len(user_name) < 6: print('输入账号太短不能小于6位') continue elif len(user_name) > 9: print('输入账号太长不能大于9位') continue elif not user_name.isalpha(): print('账号有字母注册') continue print(38*'-') user_pwd = input('请输入密码\n' '(密码由纯数字组成)\n') if len(user_name.replace(' ','')) != len(user_name): print('输入的密码有空格请重洗输入') continue elif not user_pwd.isdigit(): print('密码要纯数字') else: info = open_mode.read_all('用户注册信息.txt') if user_name in info: print('账号存在请重新注册') continue open_mode.add_info(user_name,user_pwd) print('注册成功') print(38*'-') return true
3.购物
shopping.py
import open_mode import login #login_successful[0]来判断是否登入成功 login_name = [none] goods_dict = { '0':['*',100], '1':['《nick写真集》',1], '2':['奥特曼玩偶',50], '3':['钢铁侠真人套装',10000], '4':['《产品经理强哥自传》',188], '5':['《马叉虫是如何炼成的》',888], '6':['《ywy自传》',8888888888888] } user_car = dict() def shopping(): a = '0' money = 0 if not login_name[0]: print('请先登入') return while a == '0': print(38*'-') print("\033[0;30;46m\t\t输入0是*100元\t\t\t\t\033[5m") print("\033[0;30;46m\t\t输入1是《nick写真集》1元\t\t\t\033[5m") print("\033[0;30;46m\t\t输入2是奥特曼玩偶50元 \t\t\t\033[5m") print("\033[0;30;46m\t\t输入3是钢铁侠真人套装10000元 \t\033[5m") print("\033[0;30;46m\t\t输入4是《产品经理强哥自传》18888元\033[5m") print("\033[0;30;46m\t\t输入5是《马叉虫是如何炼成的》888元\033[5m") print("\033[0;30;46m\t\t输入6是《ywy自传》8888888888888元\033[5m") print("\033[0;30;46m\t\t输入q退出程序\t\t\t\t\t\033[5m") print(38 * '-') goods_chiose = input('请输入你要购买的商品') if goods_chiose not in goods_dict: print('请好好输入') continue elif goods_chiose=='q': return else: a = '2' while true: goods_nums = input('请输入商品的数量') if not goods_nums.isdigit(): print('请输入数字') break goods = goods_dict[goods_chiose][0] price = goods_dict[goods_chiose][1]*int(goods_nums) #生成购物车 global user_car if goods not in user_car: user_car[goods] = [goods_nums,price] else: user_car[goods][0] += goods_nums user_car[goods][1] += price judge = 1 while judge == 1: print(50 * '-') jump = input('输入0结算\n' '输入1继续购买\n' '输入2清空购物车继续购买\n' '输入3清空购物车退出\n' '请输入您的选择\n' '').strip() if jump not in ['0','1','2','3']: print('请正确输入0,1,2,3中任意数字') continue if jump == '1': judge = 4 # 跳出功能选择 a = '0' # 继续购买 elif jump == '2': user_car = dict() # 清空购物车 judge = 4 # 跳出功能选择 a = '0' # 继续购买 elif jump == '3': user_car = dict() judge = 4 # 跳出功能选择 print("-" * 50) print('欢迎下次光临') elif jump == '0': judge = 4 # 跳出功能选择 print("-" * 50) #打印内容 for user_car_info in user_car.items(): print(f'{user_car_info[0]}{user_car_info[1][0]}个 合计{user_car_info[1][1]}元') money += int(user_car_info[1][1]) print(f'\t\t\t\t\t一共{money}元') print("-" * 50) print('欢迎下次光临')
4.余额查询(学了js模块再该)
balance_enquiry.py
def balance_enquiry(): pass
5.4.充值(学了js模块再该)
top_up.py
def top_up(): pass