金角大王——购物车程序

学python第三天购物车程序:

#/bin/env python3
# _*_ utf-8 _*_

List='''1. bike 800
2. cat 90000
3. book 100
'''
List_list=(("1","bike",800),("2","cat","90000"),("3","book","100"))
count = 0
sary=int(input("please input your sary:"))

while True:
    print(List)
#    Input = int(input("please input(按q退出):"))
    Input = input("please input(按q退出):")
#    print(isinstance(Input,int))
    if Input == "1":
        count = count + 800
    elif Input == "2":
        count = count + 90000
    elif Input == "3":
        count = count + 100
    elif Input == "q":
        break;

print("you buy things is",count)
if sary>=count:
    Yes=input("are you sure to buy?")
    while Yes == "y":
        sary=sary-count
        print("now,your sary is",sary)
        break
else:
    print("your sary is too small")

 

老师的写法:

#/bin/env python3
# _*_ utf-8 _*_
product_list=[
    ('Mac',9000),
    ('kindle',800),
    ('tesla',900000),
    ('python book',105),
    ('bike',2000),

]
saving=input('please input your money:')
shopping_car=[]
if saving.isdigit():
    saving=int(saving)
    while True:
        #打印商品内容
        for i,v in enumerate(product_list,1):           #enumerate 会把列表加上序号,默认从0开始;这里指定了从数字1开始
            print(i,'>>>>',v)

         #引导用户选择商品
        choice=input('选择购买商品编号[退出:q]:')

        #验证输入是否合法
        if choice.isdigit():
            choice=int(choice)
            if choice>0 and choice<=len(product_list):
                #将用户选择商品通过choice取出来
                p_item=product_list[choice-1]

                #如果钱够,用本金saving减去该商品价格,并将该商品加入购物车
                if p_item[1]

 

学了文件操作之后,购物车程序改进版:

#/bin/env python3
# _*_ utf-8 _*_
# __author__=wjz
# 需要在当前目录下建立sary.txt文件,内容存储为字典格式,用户名和金额。例如 {'wjz':1000,'wjk':500}

import time


shop_dict={'咖啡':22,'牛奶':5,'百岁山':4,'果珍':9,'甜点':15,'冰茶':11,'红茶':12}

shop_list=list(shop_dict)
shop_list.sort()
shop_tuple=tuple(shop_list)


f=open('sary.txt',"r",encoding="utf8")
sary_json=eval(f.read().strip())
f.close()
username=input("please input your name:")
sary=sary_json[username]
print("your sary is %d now." %sary)

num=0
for i in shop_tuple:
    if num==0:
        print("shop is this:")
    num+=1
    print('---> ('+str(num)+')',i,shop_dict[i])

shop_car=[]
while True:
    user_input=input("please input your select(q for exit):".strip())
    if user_input == 'q':
        break
    elif not user_input.isnumeric():
        print("please input num")
        continue
    elif int(user_input) > len(shop_tuple) or int(user_input) <= 0 :
        print("please input right num")
        continue
    else:
        user_input=int(user_input)
        select_shop=shop_tuple[user_input-1]
        if sary >= shop_dict[select_shop]:
            sary=sary - shop_dict[select_shop]
            shop_car.append(select_shop)
            print("%s has been in shopping car. sary is %d now." % (select_shop,sary))
        else:
            print("your money is not enough.")
            time.sleep(0.1)

tuple_shop_car=tuple(shop_car)
set_shop_car=set(tuple_shop_car)
buyd=""
for i in set_shop_car:
    currut_row="%s: %d" %(i,tuple_shop_car.count(i))
    buyd=buyd+"\n"+currut_row
print('''your shop car is:\n----------%s\n----------''' %buyd)

while True:
    sure_pay=input("Are you sure to pay?[y/n]")
    if sure_pay == "y":
        sary_json[username]=sary
        f=open('sary.txt','w')
        f.write(str(sary_json))
        f.close()
        print("your sary is %d now." %sary)
        break
    elif sure_pay == "n":
        break
    else:
        print("please input your selection.")

 

posted @ 2018-12-07 11:19  hacker&haidao  阅读(404)  评论(0编辑  收藏  举报