python学习第二天练习

#作业二:请闭眼写出购物车程序

#需求:
用户名和密码存放于文件中,格式为:egon|egon123
启动程序后,先登录,登录成功则让用户输入工资,然后打印商品列表,失败则重新登录,超过三次则退出程序
允许用户根据商品编号购买商品
用户选择商品后,检测余额是否够,够就直接扣款,不够就提醒
可随时退出,退出时,打印已购买商品和余额

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Site :
# @File : 第二天作业.py
# @Software: PyCharm
# @Author : XFJ
# @Time : 2018/12/4 23:28

import sys,os
msg_dic={
'apple':10,
'tesla':100000,
'mac':3000,
'lenovo':30000,
'chicken':10,
}
with open('E:\\user.txt','w',encoding='utf-8') as f :
f.write('egon|egon123\n')
f.close()
with open('E:\\user.txt','r',encoding='utf-8') as f1:
l=f1.readline().rstrip('\n').split('|')
print(l,type(l))
tag=True
count=0
count1=0
sum=0
balance=0
goods=[]

while tag:
user=input('please input your name >>: ')
user=user.strip()
if len(user)==0 or user!= l[0]:
continue
else:
while tag:
password=input('please input your password>>: ')
password=password.strip()
if len(password)==0 or password!=l[1]:
count+=1
if count==3:
tag=False
print('密码输入错误三次,请稍后重试!')
break
continue
else:
while tag:
salary=input('please input you money>>: ')
salary=salary.strip()
if len(salary)==0 or salary.isdigit() is False:
continue
else:
salary = int(salary)
print(salary)

for key in msg_dic:
print('Name: {name} Price: {price}'.format(name=key,price=msg_dic[key]))
while tag:
choice =input ('please input your choice>>: ').strip()
if len(choice)==0 or choice not in msg_dic:
print('请输入正确的商品名称!')
continue
print('Name: {name} Price: {price}'.format(name=choice, price=msg_dic[choice]))

while True:
num1 = input('please input how much do you want>>: ').strip()
if len(num1)==0 or num1.isdigit() is False:
print('请输入正确商品个数!')
continue
count1+=1
num1=int(num1)
# print(num1,type(num1))
good1={count1:[choice,msg_dic[choice],num1]}
goods.append(good1)
sum+=msg_dic[choice]*num1
# print(sum)
if salary>=sum :
balance=salary-sum
print(goods)
print(balance)
break
else:
goods.pop()
print(goods)
print(balance)
print('余额不足,请充值')
a=input('是否继续购买?继续请输入:Y/y,退出请输入:N/n\n').strip()
if len(a)==0 or a not in ['Y','y','N','n']:
continue
elif a in ['N','n']:
print(goods,balance)
tag=False
break
else:
break

猜你喜欢

转载自www.cnblogs.com/super-xfj/p/10069615.html