Python入门 第二节 作业一

# 购物车程序
# 1)启动程序后,让用户输入工资,然后打印商品列表
# 2)允许用户根据商品编号购买商品
# 3)用户选择商品后,检查余额是否够,够就直接扣款,不够就提醒
# 4)可随时退出,退出时,打印已购买商品和余额
# 5)用户入口:
# 1.商品信息存在文件里
# 2.已购商品,余额记录
# 6)商家入口:
# 1.可以添加商品,修改商品价格

商家入口
list_of_goods = [["苹果","25"],["鸡蛋","60"],["书","100"],["手机","2000"],["电脑","4000"],["自行车","800"],["耳机","150"],["足球","190"],["篮球","150"]]
f = open('D:\\catalogue.txt', 'w')
f.truncate()
f.close()
for i in range(0,len(list_of_goods)):
f = open('D:\\catalogue.txt', 'a')
f.write(list_of_goods[i][0])
f.write('\n')
f.write(list_of_goods[i][1])
f.write('\n')
f.close()
n = 1
while n > 0:
shelves = input("增加商品?")
if shelves == "yes":
goods = input("商品名称:")
price = input("商品价格:")
list_of_goods.append([goods, price])
f = open('D:\\catalogue.txt', 'w')
f.truncate()
f.close()
for i in range(0, len(list_of_goods)):
f = open('D:\\catalogue.txt', 'a')
f.write(list_of_goods[i][0])
f.write('\n')
f.write(list_of_goods[i][1])
f.write('\n')
f.close()

else:
n = 0

m = 1
while m > 0:
amend = input("修改价格?")
if amend == "yes":
goods = input("商品名称:")
price = input("商品价格:")
for i in range(0, len(list_of_goods)):
if list_of_goods[i][0] == goods:
list_of_goods[i][1] = price
break
else:
pass
f = open('D:\\catalogue.txt', 'w')
f.truncate()
f.close()
for i in range(0, len(list_of_goods)):
f = open('D:\\catalogue.txt', 'a')
f.write(list_of_goods[i][0])
f.write('\n')
f.write(list_of_goods[i][1])
f.write('\n')
f.close()
else:
m = 0

v = 1
while v > 0:
sold_out = input("下架商品?")
if sold_out == "yes":
goods = input("商品名称:")
for j in range(0, len(list_of_goods)):
if list_of_goods[j][0] == goods:
del list_of_goods[j]
break
else:
pass
f = open('D:\\catalogue.txt', 'w')
f.truncate()
f.close()
for i in range(0, len(list_of_goods)):
f = open('D:\\catalogue.txt', 'a')
f.write(list_of_goods[i][0])
f.write('\n')
f.write(list_of_goods[i][1])
f.write('\n')
f.close()
else:
v = 0

用户入口
have_been_bought = []

import os
if os.path.getsize('D:\\salary.txt'):
f = open('D:\\salary.txt', 'r')
salary = f.read()
f.close()
else:
salary = input("Salary:")

for i in range(0,len(open(r"D:\\catalogue.txt",'rU').readlines())):
with open("D:\\catalogue.txt", 'r') as x:
if i % 2 == 0:
print(i / 2)
line = x.readlines()
first_line = line[i]
second_line = line[i+1]
print( first_line,second_line)
m = 1
while m>0:
buy = input("你想要买什么?")
while buy.isdigit() is True:
if 0 < int(buy)<=(len(open(r"D:\\catalogue.txt",'rU').readlines())/2):
with open("D:\\catalogue.txt", 'r') as x:
line = x.readlines()
goods = line[2*int(buy)]
prace = line[2 * int(buy) + 1]
goods = goods.strip('\n')
prace = prace.strip('\n')
salary = int(salary) - int(prace)
if int(salary) >= 0:
have_been_bought.append([goods,prace])
f = open('D:\\shopping.txt', 'a')
f.write(goods)
f.write(prace)
f.write("\n")
f.close()
break
else:
print("余额不足!")
salary = int(salary) + int(prace)
break


else:
pass
else:
if buy == "no":
m = 0
else:
pass
f = open('D:\\salary.txt', 'w+')
f.write(str(salary))
f.close()
print("您本次购买:",have_been_bought)
print("你的余额为:",salary)
print("您总共购买了:")
g=open('D:\\shopping.txt','r')
content = g.read()
print(content)
 
 

猜你喜欢

转载自www.cnblogs.com/wang0424/p/10705284.html