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

购物车小程序

时间:2017-08-27 15:26:26      阅读:138      评论:0      收藏:0      [点我收藏+]

标签:class   lis   false   打印   logs   div   成功   com   enter   

#   __author__:Nathaniel
#   2017/6/12

# 登录
user_name = ‘name‘
password = ‘password‘
login_flag = False      # 成功登录则为True
for i in range(3):
    input_name = input(‘please input the account num:‘)
    input_password = input(‘please input the password:‘)
    # 账户校验
    if user_name == input_name and password == input_password:
        print(‘welcome!%s.‘ % user_name)
        login_flag = True
        break
    else:
        print(‘invalid account or password,you still have %d chances to try.‘ % (2-i))
else:
    print(‘where is your face?‘)

# 登陆成功 login_flag为True,进入商城
if login_flag:
    # 存放商品及其价格信息
    commodity_list = [[‘computer‘, 6300], [‘bicycle‘, 300], [‘book‘, 45], [‘bag‘, 100], [‘watch‘, 500], [‘flower‘, 999]]

    # 账户余额
    balance = int(input(‘please enter your budget:‘))

    # 存放商品的购物车
    shopping_cart = []

    # 打印商品列表
    for commodity in commodity_list:
        print(commodity_list.index(commodity) + 1, ‘. ‘, commodity[0], ‘:‘, commodity[1])

    # 开始购物
    quit_flag = True
    # 选择购买的商品编号
    commodity_num = int(input(‘Please enter the commodity number that you want to purchase:‘))
    while quit_flag:
        # 余额充足,付款
        if (balance - commodity_list[commodity_num - 1][1]) >= 0:
            print(‘%s is already added your shopping cart‘ % commodity_list[commodity_num - 1][0])
            balance = balance - commodity_list[commodity_num - 1][1]
            shopping_cart.append(commodity_list[commodity_num - 1][0])
            # 继续购买或者结算
            is_quit = input(‘Enter the commodity number to continue to purchase, press enter key to pay.‘)
            if is_quit == ‘‘:
                quit_flag = False
            else:
                commodity_num = int(is_quit)

        # 余额不足
        else:
            # 继续购买或者结算
            is_quit = input(‘your balance is not enough.‘
                            ‘Enter the commodity number to continue to purchase, press enter key to pay.‘)
            if is_quit == ‘‘:
                quit_flag = False
            else:
                commodity_num = int(is_quit)
    # 结算
    print(‘You have bought:‘, end=‘‘)
    for commodity_in_shopping_cart in shopping_cart:
        print(commodity_in_shopping_cart, end=‘;‘)
    print(‘\n your balance is %d‘ % balance)

改天新增一个2.0版本 , 增加非法输入的校验

程序小白

购物车小程序

标签:class   lis   false   打印   logs   div   成功   com   enter   

原文地址:http://www.cnblogs.com/Nathaniel-J/p/7440365.html

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