python_shopping_cart


要求

  1. 要求顾客根据商品列表选择商品
  2. 顾客可以在任何输入情况下离开程序并打印出已购商品
  3. 当顾客的余额不足时,提醒并打印出已购商品

    tips

  4. enumerate函数在打印商品列表时的作用
  5. 着重显示,改变显示字体的背景、颜色等性质

    不足

  6. 代码不够优化,不能减少代码的重复。调用函数的方法

    代码如下

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    #失败的
    '''product_list = [
    ("iphone",4800),
    ("bycicle",800),
    ("iwatch",2300),
    ("Mac pro",14800),
    ("coffee",31),
    ("surface pro",5800),
    ]
    user_salary = input("please input your salary:")
    for product,price in enumerate(product_list):
    print(product,price)
    shopping_list = []
    if user_salary.isdigit():
    list_num = input("please choose the goods number/[q]:")
    if list_num == "q":
    print("-------your shopping list-------")
    for item in shopping_list:
    print(item)
    else:
    list_num = int(list_num)
    while True:
    if list_num < len(product_list) and list_num >= 0:
    if product_list[list_num][1] <= salary:
    salary -= product_list[list_num][1]
    shopping_list.append(product_list[list_num])
    else:
    print("还买个鬼,钱不够,只剩[%s]", salary)
    print("-------your shopping list-------")
    for i in shopping_list:
    print(i)
    else:
    print("please check the goods number!")
    '''

    product_list = [
    ("iphone",4800),
    ("bycicle",800),
    ("iwatch",2300),
    ("Mac pro",14800),
    ("coffee",31),
    ("surface pro",5800),
    ]#商品列表

    shopping_cart = []#购物车

    user_salary = input("input your salary:")#钱包

    if user_salary.isdigit():
    salary = int(user_salary)
    for index, item in enumerate(product_list):
    print(index, item)
    while True:
    product_num = input("input the goood's number:")
    if product_num.isdigit():
    goods_num = int(product_num)
    if goods_num >= 0 and goods_num < len(product_list):
    if salary >= int(product_list[goods_num][1]):
    salary = salary - product_list[goods_num][1]
    shopping_cart.append(product_list[goods_num])
    else:
    print("\033[31;1msorry,you only have [%s].\033[0m"%salary)#着重显示
    break
    else:
    if goods_num == 'q':
    print("you have already quit")
    break
    else:
    print("check your input")
    continue
    else:
    if product_num == 'q':
    print("you have already quit")
    break
    else:
    print("check your input")
    else:
    if user_salary == 'q':
    exit("you have already quit")
    else:
    print("check your input")

    print("-----shopping cart list-----")
    if len(shopping_cart) == 0:
    print("shopping cart is empty")
    else:
    for index, item in enumerate(shopping_cart):
    print(index,item)
文章目录
  1. 1. 要求
  2. 2. tips
  3. 3. 不足
  4. 4. 代码如下
|