Functional Requirements:
Require users to enter the total assets, such as: Index
display list of goods, so that users select goods according to the serial number, add shopping cart
purchase, if the total amount of merchandise is larger than the total assets, suggesting that the account balance is insufficient, otherwise, the purchase success.
Additional: Can recharge, a product to remove the shopping cart
#!/usr/bin/env python #-*-coding:utf-8-*-shopping_list = [("Iphone", 5000), ("Delicious food", 48), ("Mac book", 9800), ("4800"), ("Alex python",), ("Coffee")] Shopping_cart
= [] Salary = raw_input (' Please input salary: ') if not Salary.isdigit (): print "Salary must is Digit,run again" Exit () else:salary = Int (salary) while True:print '------products list is--------' for I Ndex, item in Enumerate (shopping_list): print "\033[32m%s,%s\033[0m"% (index, item) Choice = Raw_
Input (' Please input choice[q (uit)]>>> ') if Choice.isdigit (): choice = Int (choice)
If choice < Len (shopping_list) and choice >= 0:product = Shopping_list[choice] If salary > product[1]: Confirm = raw_input (' Do your want to buy n
OW[Y/N]: ') if confirm = = ' Y ': shopping_cart.append (product) Salary-= product[1] Print for bought%s,price is%d,
Your balance is%d "% (Product[0], product[1], salary) Else: print ' Select again ' else:add_confirm = Raw_input ("You R balance is:%d, not enough, does you want to add? [y/n] "% salary) if add_confirm = = ' Y ': add_salary
= Raw_input (' Add: ') if Add_salary.isdigit ():
add_salary = Int (add_salary) Salary + = Add_salary
Print "Now balance is%d:"% salary Else:print "The money must to be digit."
Else:print "------Shopping cart list---------:"
For index, item in enumerate (Shopping_cart):
Print index, item else:print "Choice must be 0~5."
elif Choice = = ' Q ': remove_product = Raw_input ("Do you want remove product or exits now [y/n]")
if remove_product = = "Y": print "-----------your shopping cart lists-------------:"
For index, item in enumerate (Shopping_cart): Print index, item Remove_choice = raw_input (' Please input your remove choice>>> ') if remove _choice.isdigit () and int (remove_choicE) < Len (shopping_cart) and int (remove_choice) >= 0:salary + = Shopping_cart[int (rem Ove_choice)][1] del shopping_cart[int (remove_choice)] Prin T "-----------New Shopping Cart lists-------------:" For index, item in enumerate (Shoppin G_cart): Print index, item print "Your balance is %d "% salary else:print" input error, again "else : print "Exit Now" exit () Else:print "----------
-shopping cart lists-------------: "For index, item in enumerate (Shopping_cart): Print index, item print "\033[31mchoice must be digit,exit\033[0m"
The
feature is quite simple, which involves adding and deleting lists, and some logical judgments.
The results of the operation are as follows: