Novice small white Python road Day4 (shopping cart applet)

Source: Internet
Author: User

Directly on Demand:

Shopping Cart Procedure
Demand
After you start the program, let the user enter the payroll, and then print the list of items
Allow users to purchase items based on their product number
After the user selects the product, checks whether the balance is sufficient, enough on the direct debit, not enough to remind
You can exit at any time, and when you exit, print the purchased goods and balances

A relatively simple program, using a list of operations written, wrote six functions

Abnormal () to determine if the entered number is valid by throwing an exception
Commodity () take out the product parameter name price
Addshoppingcart () Add to Cart
Shopping () Judge whether the wages can buy the goods, can add to the shopping cart
Printlibray () Print List listing
Printpurchase () Print purchase list upon exit
#!/usr/bin/env python#-*-coding:utf-8-*-#Author:linghanchujian"""Shopping Cart Program requirements start the program, let the user enter the payroll, and then print the product list allows users to buy goods according to the product number of the user to select goods, check whether the balance is sufficient, enough to direct debit, enough to remind you can exit at any time, exit, print the purchased goods and balances"""Iswhile=Truecommoditylibray= [["IPhone", 5800],["Mac Pro", 12000],["Starbuck Latte", 31],["Alex Python", 81],["Bike", 800]]shoppingcart= []#Shopping CartBalance=-1#Balance"""to determine whether the entered number is valid by throwing an exception"""defAbnormal (Num):Try: Int (Num)returnTrueexceptValueError:Print()        returnFalsePass"""Remove the product parameter"""defCommodity (num,libray,number): forI,jinchEnumerate (libray):ifi==Number :returnJ[num]Pass"""Add to Cart"""defAddshoppingcart (money,number):GlobalBalance Librys= []    ifMoney >= Commodity (1, Commoditylibray, number): Balance= Money-commodity (1, Commoditylibray, number) Librys.append (Commodity (0, Commoditylibray, number)) Librys.append (Commodity (1, Commoditylibray, number)) Shoppingcart.append (Librys)Print("added to Cart!!")    Else:        Print("Insufficient balance !")    Pass"""judge whether the wages can buy the goods, can add to the shopping cart"""defShopping (wages,number):ifBalance = =-1: Addshoppingcart (Wages, number)Else: Addshoppingcart (balance, number)Pass"""Print List Listing"""defPrintlibray (libray):ifLen (libray)! =0:str=""         forI, JinchEnumerate (libray): Str= STR (i + 1) +","             forN, binchEnumerate (j): Str+ = str (b) +"  "            Print(STR)Pass"""when you exit, print the purchased items"""defprintpurchase (strinput):ifStrinput = ="N":        ifLen (shoppingcart) = =0:exit ()Else:            Print("The purchased list is as follows:") Printlibray (ShoppingCart)Print("remaining balance:"+str (balance)) exit ()PassWages= Input ("Please enter salary (n exit):") printpurchase (Wages)ifAbnormal (Wages):ifInt (Wages) <=0:Print("wages must not be less than 0") exit ()Else:    Print("wages are illegal !") whileIswhile:printlibray (commoditylibray) Commoditynumber= Input ("Please select a product number (n exit):") printpurchase (commoditynumber)ifAbnormal (commoditynumber):ifInt (commoditynumber) >0 andInt (commoditynumber) <int (len (commoditylibray) +1) :            ifBalance = =-1: Shopping (int (Wages), int (commoditynumber)-1)            Else: Shopping (balance, int (commoditynumber)-1)            Continue        Else:            Print("the number must be in 0~~"+str (Len (commoditylibray) +1) +"between !!")            Continue    Else:        Print("number is not legal !")        ContinueIswhile=false

Novice small white Python road Day4 (shopping cart applet)

Related Article

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.