天天看點

python程式設計超市購物系統_python實作簡單購物車系統(練習)

#!Anaconda/anaconda/python#coding: utf-8

#清單練習,實作簡單購物車系統

product_lists= [('iphone',5000),

('computer',6000),

('girl_friend',2000),

('boy_friend',3000)]

shop_lists=[]for i,v in enumerate(product_lists): #python的内置函數,在字典上是枚舉,列舉的意思,可以同僚獲得索引和值

print(i,v)whileTrue:

money= raw_input('請輸入你的錢數:')ifmoney.isdigit():

money=int(money)whileTrue:

choise= raw_input('請輸入商品序列号,q退出:')ifchoise.isdigit():

choise=int(choise)if choise >= 0 and choise

item=product_lists[choise]if money >= item[1]:

shop_lists.append(item)

money-=item[1]print '%s 已經加入購物車,還剩 %d 元'%(item,money)else:print '錢不夠啊!'

else:print '沒有這個商品!'

elif choise=='q':print '已經退出系統,你一共買了這些商品:'

for i inshop_lists:printiprint '還剩%d元'%money

qw= 1

break

else:print '輸入無效!'

if qw == 1:break

else:print '輸入有誤!,請從新輸入。'