码迷,mamicode.com
首页 > 其他好文 > 详细

【作业】购物车

时间:2017-09-03 11:15:52      阅读:187      评论:0      收藏:0      [点我收藏+]

标签:大循环   tle   商品   成功   作业   class   循环条件   code   log   

购物车程序

我的代码

salary = int(input(‘input your salary:‘))
shopping_car = []

shopping_list = [‘iphone‘,‘food‘, ‘book‘,‘shirt‘]
shopping_price = [5800,20,60,300]

l = shopping_list
p = shopping_price


def show_list():
    for i in shopping_list:
        print(l.index(i)+1,‘.‘,i,‘--------‘,p[l.index(i)])
    print(‘如果不想买了,输入两次“q”‘)

# def shopping():
#     show_list()
#     y = int(input(‘chose the number:‘))
#     try:
#         c.append(l[int(y - 1)])
#         for i in c:
#             s_use = sum(p[l.index(i)])
#             if s_use >= salary:
#                 print(‘too expensive!‘)
#                 break
#             else:
#                 salary = int(salary - s_use)
#         print(‘ok,you still have %d‘ % salary)
#         print(shopping_car)

    # except IndexError:
    #     print(‘can\‘t find your product.‘)
s_less = salary
while 1:
#大循环进入购物流程
    show_list()
    #展示商品列表
    try:
        #测试环节用于确认y的值,并且引导错误值退出购物流程
        #当错误值被输入,但是用户想继续购物流程,输入除q以外的值就可以继续购物

        #注意,x==‘q‘or‘n‘的写法,就相当于x==‘q‘or True
        #python中,字符串所表达的布尔值为1,所以,无论输入什么值都会进入if

        y = int(input(‘chose the number:‘))
        if (y-1)<len(l):
            shopping_car.append(l[y - 1])
            s_less = s_less - int(p[y-1])
            if s_less<0:
                print(‘you don\‘t have enough money!‘)
                shopping_car.pop()
                print(‘you have bought:‘,shopping_car)
                s_less = salary
                continue
            else:
                print(‘ok,you still have %d‘ % s_less)
                print(‘and you have bought:‘,shopping_car)
        else:
            print(‘不要乱输!重新来‘)
            continue

    except ValueError:
        print(‘pardon me? do you want to quit?‘)
        x = input(‘press "q" to quit:‘)
        if x==‘q‘:
            break
        else:
            continue

组长代码

思维导图

技术分享

代码

products = [ ‘iPhone7‘ ,‘未来简史‘ ,‘麦斯威尔咖啡‘ ,‘羊角面包‘ ,‘特仑苏牛奶‘]
prices = [ ‘5500‘ ,‘30‘ ,‘20‘ ,‘15‘ ,‘10‘]
shopping_cart = []
shopping_cart_price = []
while True:
    money_left = input( "请输入您本次购物的预算金额:" )             #未限定用户输入整型,须注意后续购物时转换为整型
    if money_left == "quit":                                      #此步骤用户亦可输入quit退出;
        print("您本次并未购物,byebye!")
        exit()
    elif money_left.isdigit() == False and money_left != quit:    #非数字且非quit,报错重新输入
        print("错误请求!只能输入预算金额或quit。")
        print("----------------------")
    elif int(money_left) > 0 :                           #数字>0,跳出循环;
        money_left = int(money_left)
        print("----------------------")
        break
    else:                                                #数字<=0,报错重新输入;
        print("错误请求!只能输入预算金额或quit。")
        print("----------------------")
while True:
    print("以下为在售商品列表:")
    for product_code in range(len(products)):                                      #以列表长度作为循环条件,便于list扩展
        print((product_code + 1), products[product_code], prices[product_code])    #编号+1显示,提升体验…
    demand = input ( ‘购买对应商品请输入编号(quit退出):‘)
    print("----------------------")
    if demand == "quit":
        if len(shopping_cart) > 0 :
            print("您的购物车内有以下商品:")
            for product_buy_code in range(len(shopping_cart)):
                print(shopping_cart[product_buy_code] ,shopping_cart_price[product_buy_code])
            print("您的余额为:",money_left ,"欢迎再来!")
            break
        else :
            print("您本次并未购物,byebye!")
            break
    elif demand.isdigit() == False and demand != quit :
        print("错误请求!只能输入商品编号或quit。")
        print("----------------------")
    elif int(demand) -1 >= 0 and int(demand) -1 < len(products) :                #之前挖的坑…
        demand = int(demand) - 1
        if int(prices[demand]) > money_left :
            print("余额不足!请选择其他商品,差额:" ,int(prices[demand]) - money_left )
            print("您的余额为:", money_left)
            print("----------------------")
        else :
            demand = int(demand)
            money_left = money_left - int(prices[demand])
            shopping_cart.append( products[demand])
            shopping_cart_price.append( prices[demand])
            print("购买成功!您的购物车内有以下商品:")
            for product_buy_code in range(len(shopping_cart)):
                print(shopping_cart[product_buy_code])
            print("您的余额为:", money_left )
            print("----------------------")
    else:
        print("错误请求!只能输入商品编号或quit。")
        print("----------------------")



【作业】购物车

标签:大循环   tle   商品   成功   作业   class   循环条件   code   log   

原文地址:http://www.cnblogs.com/scott-lv/p/7468916.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!