zoukankan      html  css  js  c++  java
  • Python3 list与循环练习(购物车)

     1 #!/usr/bin/env python3
     2 # -*- coding: utf-8 -*-
     3 # Author;Tsukasa
     4 
     5 
     6 product_list = [
     7     ('Iphone',5800),
     8     ('Mac Pro',9800),
     9     ('Bike',800),
    10     ('Watch',10600),
    11     ('Coffee',30),
    12     ('Tuskasa Python',20000),
    13 ]                                  #创建一个商品列表
    14 shopping_list = []  #创建一个购物车,空的list
    15 salary = input('你的工资是多少:')  #input让用户输入工资
    16 if salary.isdigit():
    17     salary = int(salary) #判断输入的是否数字,如果是的话改成int类型
    18     while True: #进入循环
    19         for index,list in enumerate(product_list):  #enmuerate可以提取下标,提取product_list的下标,0.1.2.3....
    20             print(index,list)   #打印出商品列表的下标和list
    21         user_choice = input('你要买什么商品:') #让用户输入购买商品的下标
    22         if user_choice.isdigit():
    23             user_choice = int(user_choice) #判断必须是数字,并int
    24             if user_choice < len(product_list) and user_choice >=0:#设定输入范围,len()可以获取list的总数目,设定总数目为上线,并大于等于0
    25                 p_list = product_list[user_choice]#通过商品下标把商品取出来
    26                 if p_list[1] <= salary: #买得起、、,判断工资够
    27                     shopping_list.append(p_list)#  添加到shopping_list购物车
    28                     salary -= p_list[1]#扣工资
    29                     print('%s 已加入购物车,你现在还有%s元。'%(p_list,salary))
    30                 else:
    31                     print('没钱你买个J8啊')
    32                     exit()
    33             else:
    34                 print('不要乱输入,请输入商品编号!')     #没有商品下标
    35         elif user_choice == "q":
    36             print('-----shopping list-----')
    37             for p in shopping_list:
    38                 print('你现在买了:',p)
    39             print('你现在还剩%s元。' %(salary))
    40             exit()
    41         else:
    42             print('不要乱输入,输入商品编号')
    为了更好
  • 相关阅读:
    推荐两个在线格式化JavaScript代码的网站
    JavaScript 获取未知对象的属性
    笔记本“电源已接通,未充电”的解决办法
    一个好的网站: StudioStyles
    Vertical Text with CSS(用CSS竖向排列文本)
    TSQL 行列互换
    获得enum中的枚举值
    恶搞百度
    想买个洗衣机,吓出了一身水……
    一个获取SQL Server数据库连接字符串的简单方法
  • 原文地址:https://www.cnblogs.com/Tsukasa/p/6539269.html
Copyright © 2011-2022 走看看