zoukankan      html  css  js  c++  java
  • Day2:购物车小程序

    一、购物车小程序第一版

    #!/usr/bin/env python
    # -*- coding:utf-8 -*-
    # Author:Hiuhung Wan
    product_list = [
        ("iphone 8", 5888),
        ("Mi Note3", 2499),
        ("Bike", 788),
        ("Phthon 3.5", 59),
        ("Mac pro", 9888),
        ("Honor 6", 599)
    ]
    notfalse = not False
    shopping_cart = []
    while notfalse:
        salary = input("Input your salary:")
        if salary.isdigit():
            salary = int(salary)
    
            while True:
                # for i in product_list:
                #     print(product_list.index(i),i)
                for index, i in enumerate(product_list):
                    print(index, i)  # 打印商品列表
                user_choice = input("Please choose what you need to buy:")
                if user_choice.isdigit():
                    user_choice = int(user_choice)
                    if user_choice < len(product_list) and user_choice >= 0:  # 用户选择了对应的商品
                        if product_list[user_choice][1] <= salary:  # 买得起
                            salary = salary - product_list[user_choice][1]  # 扣钱
                            shopping_cart.append(product_list[user_choice])
                            print("You have purchased %s, and your current balance is %s." % (
                            product_list[user_choice], salary))
                        elif user_choice == "":  # 直接按的回车,没有输入
                            print("Please enter the corresponding number of the goods!")
                        else:  # 钱不够,买不起!
                            print("Your balance is not enough!")
                    else:  # 用户输入的数字超出商品总数
                        print("Please check the commodity number!")
                elif user_choice == "q" or user_choice == "Q":  # 要退出
                    print("------------shopping list-------------")
                    for index, i in enumerate(shopping_cart):
                        print(index, i)
                    print("Your current balance is %s." % salary)
                    exit()
                else:  # 用户输入的不是数字
                    print("Please enter the corresponding number of the goods!")
    
        elif salary == "q" or salary == "Q":  # 用户想退出
            notfalse = False
        else:  # 用户输入了其他字符
            print("Please input your salary by number!")
    

      

  • 相关阅读:
    HDU 2955 Robberies(01背包)
    HDU 2602 Bone Collector(01背包)
    HUST 1352 Repetitions of Substrings(字符串)
    HUST 1358 Uiwurerirexb jeqvad(模拟解密)
    HUST 1404 Hamming Distance(字符串)
    HDU 4520 小Q系列故事――最佳裁判(STL)
    HDU 2058 The sum problem(枚举)
    【破解】修改程序版权、添加弹窗
    HDU 1407 测试你是否和LTC水平一样高(枚举)
    HDU 1050 Moving Tables(贪心)
  • 原文地址:https://www.cnblogs.com/hiuhungwan/p/7696426.html
Copyright © 2011-2022 走看看