Python Learning notes list usage exercises

Source: Internet
Author: User

#-*-coding:utf-8-*-#Author:sonnarbalance = Int (input ("Please enter 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------------") forIndex,iteminchEnumerate (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=trueshopping_list=[] whileShopping:user_choice=input ("Enter you choice \ n")    ifuser_choice.isdigit (): User_choice=Int (user_choice)ifUser_choice<len (List_of_goods) andUser_choice>=0:#prevent data from exceeding the listshopping_list.append (List_of_goods[user_choice]) temp=Balance Balance=temp-list_of_goods[user_choice][1]            ifBalance<0:#when the balance is insufficientShopping_list.pop () Balance=temp#Save the last balance                Print("Your Balance is not enough ...") Shopping=FalseElse:                Print("Your have bought", List_of_goods[user_choice][1])#print a purchased item    elifuser_choice=="Q": Shopping=FalseElse:    Print("---------List of ShoppingCart------------")     forIndex,iteminchEnumerate (shopping_list):Print(Index,item)Print("---------END of ShoppingCart------------")    Print("Your balance is [%s]"% (balance))

Run with Python 3X to implement a small example of shopping by entering a number on the list of items. The condition of passing the while loop is false, exiting, or exiting the loop in case of insufficient balance. After exiting the loop, print a list of the items you purchased.

Python Learning notes list usage exercises

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.