小程序交流群:528215729,加入和小伙伴们一起探讨开发疑难、运营idea吧

admin

admin

发表于 2018-05-13 14:11:03

摘要:购物车小程序 (微信小程序开发 - 微信 小程序外包开发-微信小程序 开发教程-微信小程序开发者社区,尽在 www.mntuku.cn)

#   __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(
 your balance is %d % balance)

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

程序小白

 

2.0版本来了 , 增加了输入校验 , 烦请小伙伴们留言找个bug

#  __author__:  Nathaniel
#  2017/8/10
user_name = name      # 用户名
user_password = password      # 密码
login_flag = True       # 标志位
for i in range(3):      # 3次错误退出程序
    input_user_name = input(please enter the user_name:)
    input_user_password = input(please enter the user_password:)
    if user_name == input_user_name and user_password == input_user_password:
        login_flag = False
        print(welcome the shopping time)
        break
    else:
        print(user_name or user_password was wrong,please try again,You have %d more chances % (2 - i))
if login_flag:
    print(There are too many errors and accounts are locked.)


if not login_flag:      # 用户名密码正确,进入购物车程序
    balance = input(please enter your budget:)        # 购物预算
    while not balance.isdigit() or int(balance) <= 0:
        balance = input(The input is invalid, please re-enter your budget:)
    balance = int(balance)
    shopping_cart = []      # 存放商品的购物车
    # 商品价格
    list_of_goods = [
        [iphone7s, 5700],
        [watch, 100],
        [book, 45],
        [computer, 6700],
        [toy, 99],
        [flower, 999],
    ]
    # 打印商品清单
    for commodity in list_of_goods:
        print(.join(
            str(list_of_goods.index(commodity) + 1) +
            :  +
            commodity[0] +
            	 +
            str(commodity[1])
        )
        )

    commodity_num = input(please enter the commodity num that you want to buy:)       # 打算购买的商品编号
    buy_flag = True
    while buy_flag:
        while True:
            if not commodity_num.isdigit():
                commodity_num = input(Please enter a valid number:)
            elif not 0 < int(commodity_num) <= len(list_of_goods):
                commodity_num = input(The product number %s does not exist, please re-enter % commodity_num)
            elif commodity_num == q:
                buy_flag = False
                break
            else:
                break

        commodity_num = int(commodity_num) - 1

        balance = balance - list_of_goods[commodity_num][1]    # 购买此商品后的余额

        if balance < 0:     # 余额不足
            balance = balance + list_of_goods[commodity_num][1]
            commodity_num = input(balance not enough.
Enter q exit, continue to purchase please enter the product number.)

        else:       # 购买成功
            shopping_cart.append(list_of_goods[commodity_num])
            print(shopping_cart,list_of_goods[commodity_num])
            commodity_num = input(Items have been added to the shopping cart.
                            Enter q exit, continue to purchase please enter the product number.)

    print(您已购买商品:, shopping_cart, 
你的余额为, int(balance))


回头复习
顺便,重构了一遍,缩短代码量,还没bug
 1 print(welcome to the shopping time!)
 2 global_flag = True
 3 while global_flag:
 4     budget = input(please enter your budget:)
 5     if not budget.isdigit() or int(budget) <= 0:
 6         print(invalid enter.,end=)
 7     else:
 8         budget = int(budget)
 9         global_flag = False
10 commodity_list = {
11     commodity1:6699,
12     commodity2:999,
13     commodity3:199,
14     commodity4:1299,
15     commodity5:1888,
16     commodity6:99,
17 }
18 
19 for key in commodity_list:
20     print(.join((key,:,commodity_list[key],¥)))
21 shop_cart = []
22 quit_flag = True
23 while quit_flag:
24     global_flag = True
25     while global_flag:
26         commodity_name = input(please enter the commodity name,enter the q quit:)
27         if commodity_name == q:
28             global_flag = False
29             quit_flag = False
30         elif commodity_name not in commodity_list:
31             print(invalid enter.,end=)
32         else:
33             global_flag = False
34             if int(commodity_list[commodity_name]) > budget:
35                 print(budget not enough.)
36             else:
37                 budget = budget - int(commodity_list[commodity_name])
38                 shop_cart.append(commodity_name)
39 
40 print(

You have purchased the goods:,end=)
41 for goods in shop_cart:
42     print(goods,,,end=)
43 print(
balance:,budget)

 

 

185阅读 | 0评论
你的回应