黑马程序员技术交流社区
标题: python购物车优化 [打印本页]
作者: 专注的一批 时间: 2019-12-11 15:47
标题: python购物车优化
#用户入口
# Author: Mr.Xue
# 2019.10.21
import ast
#shop_list = [['apple', 100], ['banana', 100], ['milk', 200], ['water', 100], ['iphone', 4000]]
thing_file = 'thing.txt'
buy_file = 'buylist_salary.txt'
buy_list = []
salary_list = []
salary = ''
thing_f = open(thing_file, 'r')
thing_info = thing_f.read()
thing_info_list = ast.literal_eval(thing_info)
thing_f.close()
buy_f = open(buy_file, 'r')
buy_info = buy_f.read()
if buy_info == '':
salary = input("input you salary:")
else:
buy_list = ast.literal_eval(buy_info)
buy_f.close()
salary = buy_list[0][1]
while not salary.isdigit():
salary = input("input you salary:")
else:
salary = int(salary)
while True:
# i = 1
# for x in shop_list:
# print(i, x)
# i += 1
for index, item in enumerate(thing_info_list):
print(index+1, item)
num = input("input which one you want buy: ")
#print(shop_list[int(num)-1])
if not num.isdigit():
if num == 'q':
print('you have bought:')
for x in buy_list:
print(x)
print('your balance have left: ', salary)
salary_list.append('salary')
salary_list.append(str(salary))
buy_list.insert(0, salary_list)
buy_list.append([''])
buy_f = open(buy_file, 'w')
buy_f.write(str(buy_list))
buy_f.close()
break
else:
print('please input "q" or num...')
else:
if int(num) <= len(thing_info_list) and int(num) > 0:
if salary < int(thing_info_list[int(num)-1][1]):
print("your balance is not enough...")
else:
buy_list.append(thing_info_list[int(num)-1])
#print(shop_list[int(num)-1], buy_list)
salary -= int(thing_info_list[int(num)-1][1])
print('you have buy %s, and you balance have left \033[31;1m%d\033[0m' % (thing_info_list[int(num)-1][0], salary))
else:
print('the thing is not exit...')
#商家入口
# Author: Mr.Xue
# 2019.10.21
import ast
caozuo = ['modify', 'delete', 'insert']
file_name = 'thing.txt'
f = open(file_name, 'r+')
info = f.read()
info_list = ast.literal_eval(info)
f.close()
while True:
i = 1
for x in caozuo:
print(i, x)
i += 1
choice = input('input your choice(1-3): ')
while True:
if choice.isdigit():
if int(choice) == 1: # modify
for j, x in enumerate(info_list):
print('\t', j+1, x)
choice2 = input('\tinput which one you want modify: ')
if choice2.isdigit():
modify_price = int(input('\tinput the modify price: '))
info_list[int(choice2)-1][1] = modify_price
elif choice2 == 'q':
break
else:
print('\tInvalid input...')
elif int(choice) == 2: # delete
for j, x in enumerate(info_list):
print('\t', j+1, x)
choice3 = input('\tinput which one you want delete: ')
if choice3.isdigit():
del info_list[int(choice3)-1]
elif choice3 == 'q':
break
else:
print('\tInvalid input...')
elif int(choice) == 3: # insert
thing_name = input("\tinput the thing's name you want insert: ")
if thing_name == 'q':
break
thing_price = int(input("\tinput the thing's price: "))
function(){ //外汇MT4教程 http://www.kaifx.cn/mt4.html
list1 = []
list1.append(thing_name)
list1.append(thing_price)
info_list.append(list1)
else:
print('Invalid value...')
elif choice == 'q':
f = open(file_name, 'r+')
f.write(str(info_list))
f.close()
print('you choice exit...')
exit()
else:
print('please input digit...')
#buylist_salary.txt
[['salary', '4500'], ['salary', '8500'], ['apple', 200], ['banana', 100], ['milk', 200], [''], ['iphone', 4000], ['']]
#thing.txt
[['apple', 200], ['banana', 100], ['milk', 200], ['water', 100], ['iphone', 4000]]
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) |
黑马程序员IT技术论坛 X3.2 |