python 学习笔记 list用法练习

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python 学习笔记 list用法练习相关的知识,希望对你有一定的参考价值。

# -*- coding: utf-8 -*-
# author:Sonnar
balance = int(input("Please enter you salary"))
List_of_goods =[[NIOXIN,188],[MAYOOU,98],[Liese,71],[Aerogard,69],
                [AMOS,139],[Tide,29],[Crest3D,219],[AEON,60],
                [Morocanoil,298],[EMART,128],[NIOXIN,210],[Fino,118]]
print("Welcome to the shopping system ...")
print("---------List of goods------------")
for index,item in enumerate(List_of_goods):
    print(index,"\t",item)
print("---------END of Goodslist------------")
print("\033[1;32;0mPlease enter the number or the goods you want to buy...\033[0m")
print("\033[1;31;0mIf you don‘t want to buy it,you can enter‘Q’leave...\033[0m")

shopping=True
Shopping_List=[]
while shopping:
    User_Choice=input("Enter you choice \n")
    if User_Choice.isdigit():
        User_Choice=int(User_Choice)
        if User_Choice<len(List_of_goods) and User_Choice>=0: #防止数据超出列表
            Shopping_List.append(List_of_goods[User_Choice])
            temp=balance
            balance=temp-List_of_goods[User_Choice][1]
            if balance<0:#当余额不足
                Shopping_List.pop()
                balance=temp #将上一次的余额保存
                print("Your balance is not enough...")
                shopping=False
            else:
                print("Your have bought ",List_of_goods[User_Choice][1])#打印购买的商品
    elif  User_Choice=="Q":
        shopping=False
else:
    print("---------List of ShoppingCart------------")
    for index,item in enumerate(Shopping_List):
        print(index,item)
    print("---------END of ShoppingCart------------")

    print("Your balance is [%s]"%(balance))

用 python 3X运行,实现在商品列表上,输入编号来实现购物的小示例。通过while 循环的条件为False,退出,也能在balance不足的情况下 退出循环。退出循环后,打印选购商品的列表。

以上是关于python 学习笔记 list用法练习的主要内容,如果未能解决你的问题,请参考以下文章

学习笔记:python3,代码片段(2017)

Python的list用法笔记

学习笔记30Python基础综合练习

Python学习—字符串练习

云计算学习笔记,云计算Python自动化基本用法

python基本数据结果及用法(str,list,tuple,doct,set) 笔记