购物车程序

购物车程序 (一):

整体流程

拆件流程:

1.输入工资

salary=input("Input your Money")
if salary.isdigit():  #判断是否为数字
   salary=int(salary)
else:
exit("Invalid data type...")

welcom_msg='Welcom to You to purchse Ball'.center(50,'-')
print(welcom_msg)

 

2. 列出所有的商品

#To list all your goods

exit_flag= False #用于结束while true语句的变量

production_list=[   #列出所有的商品的“名字” “价格”
('Ball Head',1000),
('Ball Body',3000),
('Ball JJ',10),
('Ball Hands',1000),
('Ball Feet',500)]

#Circle Goods

while exit_flag is not True:  #开始无限循环
print("production list".center(50,'-'))  #打印production list的标题,用"-"居中50个字符
for item in enumerate (production_list):  #用于列表的地标排序
index=item[0]                 #用于列出地标
ball_name=item[1][0]  #用于列出商品的名字
ball_price=item[1][1]  #用于列出商品的价格  
print(index,ball_name,ball_price) 
user_choice=input("q or quite" "," "What do you want to buy?") #如果停止循环的方法,就是输入文字


3. 购买,显示购买的商品和价格

if user_choice.isdigit():
user_choice=int(user_choice)
if user_choice < len(production_list): #代表长度不能超过列表的数字
p_item= production_list[user_choice] #当你选择地标时,会延伸出他的商品名字和价格
if p_item[1] < salary:  
salary= salary-p_item[1]  #减少salary
shop_car.append(p_item) #追加到shop car

print("BALL [%s] into your shop car, and your left money is [%s]" %(p_item,salary)).

5.不买,显示余额和商品

else:
print("Your [%s], can't afford 球晓松, please provide more" %(salary))
else:
if user_choice=='q' or user_choice == 'quite':
exit_flag = True   #循环结束
print("Your current salary [%s]. Ball have daded!" %(salary))
for item in shop_car:
print(item)
print("End".center(40,'-'))

 

购物车程序(优化):

1. 要求可以购买多件物品,并且以多件为价格

p_item = production_list[user_choice]  #接着(3. 购买,显示购买的商品和价格)下文
continue_i = input("How many do you want to buy?")
if continue_i.isdigit():
continue_i = int(continue_i) 
B = p_item[1] * continue_i  #拿单价乘以数字

 

 

2. 记录购买的时间

import datetime #调出时间的命令
time=datetime.datetime.now() #时间的命令
now_time=time.strftime('%c') #%c 以本地时间显示日期和时间

 

3.转换信息

 

read_username=[salary,p_item[0],B,continue_i,now_time,username]
print(read_username)

import json # 使用json

jsobj = json.dumps(read_username) # 使用json.dumps进行转换 str
username_salaryfile = open('accoujnt.txt', 'a') # 打开文本
username_salaryfile.write(jsobj + '\n') # 使用+'\n'空一行

 

4.读取(3.转换信息)

 

 

 

username_salaryfile = open('accoujnt.txt', 'r') # 打开文本
for line in username_salaryfile.readlines(): # 使用遍历把文本内容拆分出来,使用readlines命令
import json # 使用json再把内容解码一遍
jsobb=json.loads(line)
for key in enumerate(jsobb): # 遍历内容?这里有一个问题,为什么永远只能搜索到最后一个账户信息
print(key)
username = input("input your username:")
if username in jsobb[5]:
ask_salary = input("do you want to add more salary?")
if ask_salary == "y" or ask_salary=="yes":
add_moresalary = input("Input the money")
if add_moresalary.isdigit():
add_moresalary = int(add_moresalary)
adding_salary = add_moresalary + jsobb[0] # 使用V的内容加上原本在文本的金额
salary = adding_salary
print(adding_salary)
else:
print("Wrong input")
else:
salary=jsobb[key]

posted @ 2016-12-03 16:59  Daniel.lu  阅读(199)  评论(0编辑  收藏  举报