python學習第二天練習
阿新 • • 發佈:2018-12-05
#作業二:請閉眼寫出購物車程式
#需求:
使用者名稱和密碼存放於檔案中,格式為: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