作业要求:
1、启动程序后,输入用户名密码登录
2、显示用户余额
3、打印商品列表
4、允许用户根据编号购买商品(卧槽,没有看到,下次更新)
5、用户选择商品后,检测余额是否足够
6、扣除,不够就提醒余额不足
7、扣除,够就扣款,更新余额信息
Python Code
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# @Time : 2017/8/7 9:45
# @Author : Shope
# @QQ :215522879
# @My Blog : http://blog.sina.com.cn/u/2047118510
# @File : shoping.py
# @Software: PyCharm
# user=list()
# fruit={}
# k=1
# v=1
# i=0
# flag=True
# count=0
# choice,list1=[],[]
# sum1,sum2,sum3=0,0,0
# m,l1=0,0
# with open('message.txt','r') as f:
# # frist=f.readline()
# # print(frist[1][0:])
# for line in f:
# # frist=f.readline()
# # print(frist)
# user.append(line.strip().split())
# print(user)
def fruit_def():
k = 1
v = 1
global fruit
fruit={}
# 输出所有商品信息:
with open('shop.txt', 'r') as f:
for line in f:
k, v = line.strip().split()
fruit[k] = v
#print(fruit)
print("商品信息:")
for k1 in fruit:
print("%s:%s¥" % (k1, fruit[k1]))
#print(fruit['BitterOrange'])
def choice_def():
choice, list1 = [], []
sum1, sum2, sum3 = 0, 0, 0
m, l1 = 0, 0
# 购买选择
while True:
goods = raw_input("请输入选择商品(N/n退出):")
if goods == 'n' or goods == 'N':
# print("欢迎下次使用!")
break
else:
for k1 in fruit:
if goods == k1:
break
else:
print("商品不存在!请重新选择")
continue
amount = input("请输入购买商品数量:")
# 判断购物车是否为空,为空的话将购买商品加入购物车
if len(choice) == 0:
list1 = [goods, int(fruit[goods]), amount, amount * int(fruit[goods])]
else:
# 检测如果商品存在,输入的参数替换之前的参数
for m in range(len(choice)):
print(choice[m][0])
if goods == choice[m][0]:
#print(222)#调试使用,看是否循环到这里
#print m
choice[m][0:] = [goods, int(fruit[goods]), amount, amount * int(fruit[goods])]
# 否则的就将新的商品录入list1列表
else:
list1 = [goods, int(fruit[goods]), amount, amount * int(fruit[goods])]
# 判断商品是否存在,存在就跳过,不存在就添加到购物车
for m1 in range(len(choice)):
if goods == choice[m1][0]:
break
else:
choice.append(list1)
print("已选商品信息:%s,价格:%s,数量:%s,总价:%s" % (goods, int(fruit[goods]), amount, amount * int(fruit[goods])))
print("你已选择%s种商品" % len(choice))
# 结算
if len(choice) > 0:
print("[商品,单价,数量,总价]")
for l in choice:
print(l)
sum1 = sum1 + l[2]
sum2 = sum2 + l[2] * l[1]
print("总量:%s,总价:%s" % (sum1, sum2))
sum3 = int(bal) - sum2
# print(sum3)
exit1 = raw_input("输入Y\y确认交易,输入其他取消交易:")
if exit1 == 'y' or exit1 == 'Y':
if sum3 > 0:
print("你要有可用余额%s" % sum3)
# 更新用户余额信息
with open("message.txt", "r+") as f:
line1 = f.readlines()
# 将原来的文件读取出来,写入line1里面
with open("message.txt", "w+") as f:
# 将line1里面的每一行分别赋值给line,for循环
for line in line1:
if line.find(user) >= 0:
# 如果循环到当前用户,执行余额替换,更改余额信息
s = line.replace(str(bal), str(sum3))
f.writelines(s)
continue
# continue跳出循环,继续将其余的line写入message.txt文件
f.writelines(line)
else:
print("金额不足!!!!!!")
else:
print("欢迎下次使用!")
#login用户登录:
def main():
global user
user = list()
i = 0
flag = True
global bal
bal=0
with open('message.txt', 'r') as f:
while flag == True and i < 3:
name = raw_input("请输入用户:")
psd = raw_input("请输入密码:")
for line in f:
user, passwd, bal = line.strip().split()
#bal1=int(bal)
if name == user and psd == passwd:
print("欢迎使用某某某超市系统!!")
print("%s账户余额%s¥\n" % (user, bal))
flag = False
fruit_def()
choice_def()
break
if flag == True:
i += 1
print("重新输入用户!")
else:
break
if i < 1:
pass
else:
print("失败次数过多,请下次再来!")
if __name__ == '__main__':
main()
message.txt文件用来记录用户信息
用户、密码、余额
shope 123456 287
alex 654321 6451
eric 111111 10571
S 1 9907
执行情况:
C:\Python27\python.exe F:/python/oldboy12/day2/shoping_homewk.py
请输入用户:shope
请输入密码:123456
欢迎使用某某某超市系统!!
shope账户余额827¥
商品信息:
BitterOrange:22¥
Gooseberry:6¥
Cranberry:33¥
Avocado:7¥
Walnut:15¥
Papaya(Pawpaw):37¥
Melon:19¥
Loquat:41¥
Tangor:13¥
Sapodilla:43¥
Orange:5¥
Lemon:38¥
Pear:24¥
Blueberry:23¥
Chestnut:27¥
Cumquat:21¥
Honey-dew:14¥
Sugarcane:11¥
FlatPeach:40¥
Rambutan:17¥
CustardApple:8¥
LotusNut:28¥
Peanut:18¥
Olive:12¥
Raspberry:10¥
A:5¥
C:7¥
B:6¥
E:9¥
D:8¥
Foxnut:20¥
Plum:25¥
Water-chestnut:32¥
Strawberry:4¥
Mango:34¥
Blackberry:16¥
Pineapple:3¥
Betelnut:2¥
Apple:1¥
WildPeach:35¥
Bilberry:39¥
WaterCaltrop:29¥
Sorosis:44¥
KiwiFruit:36¥
Grape:42¥
Lichee:26¥
Guava:9¥
Durian:30¥
Longan:31¥
请输入选择商品(N/n退出):A
请输入购买商品数量:100
已选商品信息:A,价格:5,数量:100,总价:500
请输入选择商品(N/n退出):B
请输入购买商品数量:20
已选商品信息:B,价格:6,数量:20,总价:120
请输入选择商品(N/n退出):C
请输入购买商品数量:30
已选商品信息:C,价格:7,数量:30,总价:210
请输入选择商品(N/n退出):D
请输入购买商品数量:20
已选商品信息:D,价格:8,数量:20,总价:160
请输入选择商品(N/n退出):A
请输入购买商品数量:10
已选商品信息:A,价格:5,数量:10,总价:50
请输入选择商品(N/n退出):N
你已选择4种商品
[商品,单价,数量,总价]
['A', 5, 10, 50]
['B', 6, 20, 120]
['C', 7, 30, 210]
['D', 8, 20, 160]
总量:80,总价:540
287
输入Y\y确认交易,输入其他取消交易:Y
你要有可用余额287