zoukankan      html  css  js  c++  java
  • 003列表和元组作业

     1 #__author:"_nbloser"
     2 #date: 2017/11/22
     3 
     4 salary = input("请输入你的工资:")
     5 if salary.isdigit() != True:
     6     print("你玩野!自动退出")
     7     exit("你必须输入数字")
     8 else:
     9     salary = int(salary)
    10 shopping_cart = []
    11 
    12 balance = salary
    13 while(True):
    14     goods = ["iphone6s","mac book","coffee","python book","bicydle"]
    15     price = [5800,9000,32,80,1500]
    16 
    17     for i in range(5):
    18         print(i+1,goods[i],price[i])
    19     num = input("请输入输入你要买的东西的号码:")
    20 
    21     if num.isdigit()!=True:
    22         print("你玩野!自动退出")
    23         exit("你必须输入数字")
    24     else:
    25         num = int(num)
    26     if num>len(goods):
    27         break
    28     balance = balance - price[num-1]
    29     if balance>0:
    30         print("已加入",goods[num-1],"到你的购物车,当前余额:",balance)
    31         shopping_cart.append(goods[num-1])
    32         shopping_cart.append(price[num-1])
    33     else:
    34         print("余额不足,",balance,",请重新选择")
    35         balance += price[num-1]
    36 
    37 print("你已购买以下商品:")
    38 for x in shopping_cart:
    39     print(x,end="  ")
    40     if 0!=shopping_cart.index(x)%2:
    41         print()
    42 
    43 print("你的余额为:",balance)
    44 print("欢迎下次光临!")

    使用嵌套goods=[["iphone6s",5800],["macbook",9000],["coffee",32],["pythonbook",80],["bicycle",1500]]    里面使用元组更好
    #列表长度
    len(goods)
    #判断int类型
    ifnum.isdigit()!=True:

    #enumerate
    for k,i in enumerate(goods,1):
        print(k,end="")
        forjinrange(len(i)):

    exit("你必须输入数字")

     1 #__author:"_nbloser"
     2 #date: 2017/11/22
     3 
     4 salary = input("请输入你的工资:")
     5 if salary.isdigit() != True:
     6     print("你玩野!自动退出")
     7     exit("你必须输入数字")
     8 else:
     9     salary = int(salary)
    10 shopping_cart = []
    11 
    12 balance = salary
    13 while(True):
    14     goods = [("iphone6s",5800), ("mac book",9000), ("coffee",32), ("python book",80), ("bicycle",1500)]
    15 
    16     for k,i in enumerate(goods,1):
    17         print(k,end="  ")
    18         for j in range(len(i)):
    19             print(i[j], end="  ")
    20         print()  #enumerate(goods,1)
    21     num = input("请输入输入你要买的东西的号码:")
    22 
    23     if num.isdigit()!=True:
    24         print("你玩野!自动退出")
    25         exit("你必须输入数字")
    26     else:
    27         num = int(num)
    28     if num>len(goods):
    29         break
    30     balance = balance - goods[num-1][1]
    31     if balance>0:
    32         print("已加入",goods[num-1][0],"到你的购物车,当前余额:",balance)
    33         shopping_cart.append(goods[num-1])
    34     else:
    35         print("余额不足,",balance,",请重新选择")
    36         balance += goods[num-1][1]
    37 
    38 print("你已购买以下商品:")
    39 for x in shopping_cart:
    40     for j in range(len(x)):
    41         print(x[j], end="  ")
    42     print()
    43 
    44 print("你的余额为:",balance)
    45 print("欢迎下次光临!")
    代码优化后

    #元组##
    元组可以看成是可变的列表
    #定义
    a=(1,2,6,4,3,7,9,5,3)
    要是只有一个元素,要加上一个逗号
    b=("hua",)

  • 相关阅读:
    转: 关于linux用户时间与系统时间的说明
    转: 关于CAS cpu锁的技术说明。
    基于TCPCopy的Dubbo服务引流工具-DubboCopy
    Netty中的坑(下篇)
    编写明显没有错误的代码
    Zookeeper-Zookeeper client
    Zookeeper-Zookeeper leader选举
    Zookeeper-Zookeeper启动过程
    Zookeeper-Zookeeper的配置
    Zookeeper-Zookeeper可以干什么
  • 原文地址:https://www.cnblogs.com/-nbloser/p/7901295.html
Copyright © 2011-2022 走看看