Requirements:
# 1. Users give their accounts charge money: for example, to charge 3,000 yuan.
# 2 page displays the serial number + name + price, such as:
# # [=========== following commodity for you to choose: ===========]
## No. Name price
# 1 # 1999 PC
# 2 mouse # 10
# 20 # yacht 3
# 4 # 998 beauty
# N # n-cart or billing
# q or Q to exit the program (e.g., billing cart can not exit)]
# [== ========================================]
# cart settlement
# 3. user input select the product number, and then print trade names and commodity prices, and this product, add to cart, the user can continue to add products.
# 4. If the product number entered by the user is incorrect, you are prompted an error, and re-enter.
# 5. (1) user input n for the shopping cart settlement, in order to display the user inside the shopping cart of goods, quantity and unit price
# (2) If the amount of money is insufficient recharge allow users to delete an item until it can be purchased, if recharge plenty of money, you can purchase exit
# (3) after exiting the program, followed by the display of items purchased, quantity, unit price, and the total of how much money to spend, how much the balance of the account
# 6. user input Q or q to exit the program .
Can be divided into modules on the line to write,
# Product List
goods_list = [ {"name": "电脑", 'price': 1999}, {"name": "鼠标", 'price': 10}, {"name": "游艇", 'price': 20}, {"name": "美女", 'price': 998}, {"name": "油精", 'price': 30}, ]
# Initial Amount
money = 0
# Cart (you ask cart, showing roughly what it was like)
car = {} ''' car = { 1: {"name": "电脑", 'price': 1999}, 2: {"name": "鼠标", 'price': 10}, 3: {"name": "游艇", 'price': 20}, 4: {"name": "美女", 'price': 998}, 5: {"name": "油精", 'price': 30}, } '''
# Recharge
def chongzhi(): global money while 1: MONEY1 = the INPUT ( ' Enter the amount you want to recharge: ' ) .strip () IF money1.isdigit (): MONEY1 = int (MONEY1) Money + = MONEY1 Print ( ' successful recharge, recharge amount of this element% s,% d-membered total amount of ' % (MONEY1, Money)) BREAK the else : Print ( ' recharge failed. Please enter the number ' )
# Show merchandise
def show_goods(): print('序号 名称 价格') for index, dic in enumerate(goods_list, start=1): print(index, dic['name'].rjust(6), str(dic['price']).rjust(5))
#Add to cart
DEF Shopping (sn): # This already exists inside the shopping cart IF sn in CAR: CAR [Sn] [ ' AMOUNT ' ] +. 1 = # does not exist, create a data the else : car[sn] = { 'name': goods_list[sn - 1]['name'], 'price': goods_list[sn - 1]['price'], 'amount': 1 } Print ( ' Add cart success ' ) Print ( ' No. Name Price Quantity Total ' ) Print (sn, CAR [sn] [ ' name ' ] .rjust (6), str (CAR [sn] [ ' . price ' ] ) .rjust (. 5), STR (CAR [Sn] [ ' AMOUNT ' ]). the rjust (. 4 ), str(car[sn]['price'] * car[sn]['amount']).rjust(6))
# Show cart
def show_car(): Total = 0 Print ( ' [=================== cart detail below ===================] ' ) Print ( ' number name Unit quantity total ' ) for K, DIC in car.items (): Print (K, DIC [ ' name ' ] .rjust (. 6), STR (DIC [ ' . price ' ]). the rjust (. 5), STR (DIC [ ' AMOUNT ' ]). the rjust (. 4 ), str(dic['price'] * dic['amount']).rjust(6)) total += dic['price'] * dic['amount'] return total
# Display of goods purchased
DEF show_shopping (): Print ( ' [=================== your purchase details are as follows ================ ===] ' ) Print ( ' number name Unit quantity total ' ) for K, DIC in car.items (): Print (K, DIC [ ' name ' ] .rjust (. 6), STR (DIC [ ' . price ' ]). the rjust (. 5), STR (DIC [ ' AMOUNT ' ]). the rjust (. 4 ), str (dic [ ' . price ' ] * dic [ ' AMOUNT ' ]). rjust (6 )) Print ( ' shopping success! ' )
# drop out
DEF quit (): , Ltd. Free Join Flag Print ( ' [=================== always welcome ================ ===] ' ) flag = False
# Delete item
def del_goods(sn): if sn.isdigit(): sn = int(sn) if 0 < sn <= len(goods_list): car[sn]['amount'] -= 1 if car[sn]['amount'] == 0: car.pop(sn) else: error2() else: ERROR1 ()
# Settlement
def jiesuan(): global flag while 1: Total = show_car () Print ( ' your current amount is% d yuan, into your cart's total commodity price of RMB% d ' % (Money, Total)) # Enough money, you can pay IF Money - Total> = 0: RES = the INPUT ( ' confirming the purchase, press y, according to q end the program, press any key to continue shopping ' ) .strip () IF res.upper () == ' the Y- ' : Print ( ' purchase is successful, your remaining% d yuan ' % (Money - Total)) show_shopping() quit() break elif res.upper() == 'Q': quit() Flag = False BREAK the else : BREAK # enough money, delete some commodities the else : RES = the INPUT ( ' your balance is insufficient, please delete some of the goods or recharge d c ' ) IF res.upper () == ' D ' : sn = the INPUT ( ' Please enter the article number you want to delete: ' ) .strip () # remove items del_goods (sn) elif res.upper () == ' C ' : chongzhi ()
# Some error function
DEF ERROR1 (): Print ( ' the options you entered does not exist ' ) DEF Error2 (): Print ( ' option you entered is out of range ' )
# Program entry
IF __name__ == ' __main__ ' : Print ( ' [=================== Welcome ** Mall ============= ======] ' ) # 1 top- Chongzhi () # 2. display of goods show_goods () # 3. start shopping Flag = True the while Flag: sn = the INPUT ( ' Please enter the number you want to purchase goods :( settlement by n, press q to exit) ' ) .strip () IF sn.isdigit (): sn = int(sn) if 0 < sn <= len(goods_list): # 购物 shopping(sn) else: error2() elif sn.upper() == 'Q': quit() elif sn.upper() == 'N': jiesuan () else: ERROR1 ()
summary:
1. Global variables used in the flag while flag in accordance with the wording before I would while True: After written like this, if they were in the loop
(1) if it can not meet certain conditions need to exit the loop, there is no way to do, write a variable, then it is a good control loop access, flag = 0, quit.
Such as:
flag = 1
while flag:
if(i<5):
flag=0
After executing the condition if, it will be out of the loop.
(2) the second case
flag = 1
while flag:
for in ilistvar:
pass
flag = 0
break
如果遇到执行循环之后想跳出大循环的这种情况,需要在内部循环中先使用break,跳出当前玄幻,再次循环的时候flag=0,就会立马退出。
2.程序中的全局变量money,flag 全局变量若要在函数里面就行修改的时候必须使用关键字,global。
3,.判断字符串中是否是纯数字字符串函数isdigit(),还有在字符串前面添加空白间隔字符,rjust().
4.
if sn.isdigit(): sn = int(sn) if 0 < sn <= len(goods_list): # 购物 shopping(sn) else: error2() elif sn.upper() == 'Q': quit() elif sn.upper() == 'N': jiesuan() else: error1()
思路很重要,一定要清楚。多成判断,处理每一层。
5.enumerate(可迭代数据,起始值) 返回的是一个个元祖索引和值的小元祖。
for k,v enumerate(XX):
print(k,v)
6.程序先考虑大体思路,然后可以从怎么去使用入手写程序。
7.本题程序,购物车的构造,以及商品列表的构造都是需要注意的。