zoukankan      html  css  js  c++  java
  • python列表、字典、循环结构练习题

    1. 购物车程序:
      需求:
    • 启动程序,让用户输入工资,然后打印商品列表;
    • 允许用户根据商品编号购买商品;
    • 用户输入商品列表后检测余额是否足够,够就直接扣款,不够就提醒;
    • 用户可以一直购买商品,也可以直接退出,退出后打印已购买商品和余额;
    #第一题
    salary = int(input('Please input your salary:'))
    products = [['Iphone8',6888],['MacPro',14800],['小米6',2499],['Coffee',31],['Book',80],['Nike shoes',799]]
    shopping_cart = list()
    while True:
        print('-------- 商品列表 --------')
        for index,i in enumerate(products):
            print('%s. %s %s'%(index,products[index][0],products[index][1]))
        choice = input('Please input product num:')
        if choice.isdigit():
            choice = int(choice)
            if choice >=0 and choice < len(products):
                if salary >= products[choice][1]:
                    shopping_cart.append(products[choice])
                    salary -= products[choice][1]
                else:
                    print('余额不足')
                    print('----- 已购买商品 -----')
                    for index,p in enumerate(shopping_cart):
                        print('%s. %s %s'%(index,shopping_cart[index][0],shopping_cart[index][1]))
                    print('余额',salary)
                    break
            else:
                print('商品不存在')
        if choice == 'q':
                print('----- 已购买商品 -----')
                for index,p in enumerate(shopping_cart):
                    print('%s. %s %s'%(index,shopping_cart[index][0],shopping_cart[index][1]))
                print('余额',salary)
                break
    
  • 相关阅读:
    怎样让一个div高度自适应浏览器高度
    angular change the url , prevent reloading
    论习惯的重要性
    php的几个内置的函数
    cakephp 2.0 源码解读(一)
    svn switch 的用法
    浏览器兼容问题 及 解决方案 (一)
    浏览器兼容问题 及 解决方案 (二)
    promise 承诺
    ng-selected 与ng-options的使用
  • 原文地址:https://www.cnblogs.com/stacks/p/7783851.html
Copyright © 2011-2022 走看看