首页 > 代码库 > python 学习笔记 list用法练习
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用法练习
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。