(这是网课中的一个python程序,我上传上自己写的,也没有别的意思,就是想记录自己的学习过程,也对学到的知识加深理解)
简易版
功能要求:
1.启动程序后,让用户输入工资,然后打印商品列表
2.允许用户根据商品编号购买商品
3.用户选择商品后,检测余额是否够,够就直接扣款,不够就提醒
4.可随时退出,退出时,打印已购买商品和余额
程序如下:
_author_ = 'zhangzhengrecheng'
product_list = [
('coffee',30),
('bike',599),
('book',49),
('earphone',88),
('pen',99),
('computer',3499),
]
shopping_list=[]
salary = input("please input your salary:")
if salary.isdigit():
salary = int(salary)
while True:
for index,item in enumerate(product_list):
#print(product_list.index(item),item)
print(index,item)
user_choice = input("请选择你好购买的商品:")
if user_choice.isdigit():
user_choice = int(user_choice)
if user_choice < len(product_list) and user_choice >=0:
p_item = product_list[user_choice]
if p_item[1] <= salary:
shopping_list.append(p_item)
salary -= p_item[1]
print("added %s into shopping cart,your balance is %s"%(p_item,salary))
else:
print("你的余额只剩%s,无法购买" % salary)
else:
print("商品不存在")
elif user_choice == 'q':
print('--------shopping list----------')
for p in shopping_list:
print(p)
print("您的余额是:",salary)
exit()
else:
print("您的salary 不足,无法购买,sorry")
升级版
概述:
本次作业文件夹一共包含以下个文件:
流程图1:购物车思路流程图
流程图2:用户登陆流程图
程序文件:shopping_cart.py
用户信息文件:userinfo.txt
历史购物文件:history_shopping.txt
程序说明文件:README.txt
一,程序功能:
基础要求:
1、启动程序后,输入用户名密码后,让用户输入工资,然后打印商品列表
2、允许用户根据商品编号购买商品
3、用户选择商品后,检测余额是否够,够就直接扣款,不够就提醒
4、可随时退出,退出时,打印已购买商品和余额
5、在用户使用过程中, 关键输出,如余额,商品已加入购物车等消息,需高亮显示
扩展需求:
1、用户下一次登录后,输入用户名密码,直接回到上次的状态,
即上次消费的余额什么的还是那些,再次登录可继续购买
2、允许查询之前的消费记录
二,***********文件说明***************
在运行程序之前,一定要检查用户信息文件userinfo.txt和历史购物文件history_shopping.txt
里面是否包含{},如果没有包含,请务必加上,不然程序会报错。
三,部分变量说明
user_file指读取的用户信息记录
history_file指读取的历史购物记录
username指用户姓名
password指用户密码
salary指用户工资
shoppinglist_dict指读取的历史购物记录
number指用户购买商品的编号
user_choice指是否选择查询历史购物记录
shoppinglist_now指本次购物记录
四,运行代码
本程序的开发环境是python3.x
运行后,根据控制台显示的提示信息执行
#_*_coding:utf-8_*_
exit_flag =False
user_file =open("userinfo.txt",'r+') # 读取用户信息记录
user_info = user_file.read()
user_info=eval(user_info)
username = input("请输入您的账号:")
password = input("请输入您的密码:")
while True:
if username in user_info:
if password in user_info[username]:# 密码如果能对应用户名,就欢迎登录
salary = int(user_info[username][password])
print("\033[32;1m欢迎登陆,当前余额为%s\033[0m"%salary)
break
else:
password = input("\033[31;1m密码输入错误,请重新输入:\033[0m")
continue
else:
password_salary = {}
salary = input("欢迎首次登录,请输入您的工资:")
if salary.isdigit():
salary = int(salary)
with open("userinfo.txt","r+") as wirte_userinfo:
password_salary[password] =salary
user_info[username] = password_salary
user_info =str(user_info)
wirte_userinfo.seek(0)
wirte_userinfo.write(user_info)
wirte_userinfo.tell()
break
else:
print("\033[31;1m工资请输入数字,请重新输入:\033[0m")
continue
goods = [
{"name": "电脑", "price": 1999},
{"name": "鼠标", "price": 10},
{"name": "游艇", "price": 20},
{"name": "美女", "price": 998},
]
history_file = open("history_shopping.txt","r+") # 读取历史购买记录文件
history_shopping = str(history_file.read())
shoppinglist_dict = eval(history_shopping) # 将历史记录信息转换为字典
if username not in shoppinglist_dict:
shoppinglist_dict[username] = []
shoppinglist = shoppinglist_dict[username] # 不是首次登录,将之前历史记录赋值到变量
shoppinglist_now = [] # 本次购物记录留空
user_choice = input("\n是否选择查询历史购物记录(y/n):")
if user_choice == 'y':
print('历史购物记录'.center(40,'-'))
print(shoppinglist)
print('结束'.center(40,'-'))
else:
print('谢谢您的信任,请继续购物'.center(40,'-'))
while not exit_flag:
print('goods'.center(40,'-'))
for index,item in enumerate(goods):
print(index,item)
print('end'.center(40,'-'))
number = input("请输入要想购买的商品编号(或者按q直接退出):")
if number == 'q':
exit_flag =True
if type(user_info) == str:
user_info = eval(user_info)
else:
pass
user_info[username][password] =str(salary)
user_file.seek(0)
user_file.write(str(user_info))
user_file.tell()
user_file.close()
print("goods".center(40,'-'))
print(shoppinglist_now)
print("\033[32;1m当前余额为%s\033[0m" % salary)
shoppinglist.extend(shoppinglist_now)
shoppinglist_dict[username] =shoppinglist
history_file.seek(0)
history_file.write(str(shoppinglist_dict))
history_file.tell()
history_file.close()
elif number.isdigit() == False:
print("\033[31;1m您的输入不是商品编号,请输入商品编号\033[0m")
elif int(number)>=(len(goods)) or int(number) < 0:
print("\033[31;1m您所购买的商品不在购物清单\033[0m")
else:
number1 =int(number)
if goods[number1].get('price') <(salary):
salary -= goods[number1].get('price')
print("\033[1;34;47m添加[ %s ]到您的购物车,购物之后您的余额是[ %s]\033[0m" % (
goods[number1].get('name'), salary))
shoppinglist_now.append(goods[number1])
else:
print("\033[1;34;47m您的余额只有%s,无法购买\033[0m" % salary)