python基礎程式設計簡單案例 購買商品

2021-09-16 13:45:56 字數 2286 閱讀 5204

goods_dict =

,"002":,

"003":,

"004":,

}def

goods_info()

:print

('-'*20

+'列印商品列表:'

+'-'*20

)print

('商品編號\t商品名稱\t商品**'

)for i in goods_dict.keys():

print

("{} \t{} \t{}"

.format

(i,goods_dict[i]

['name'

],goods_dict[i]

['price'])

)orders =

i=0deforder_add()

:global i

i =i +

1 good_index =

input

('請輸入商品編號:'

)while good_index not

in goods_dict.keys():

good_index =

input

('請輸入正確商品編號:'

)else

:while1:

try:

good_num =

int(

input

('請輸入商品數量:'))

except

:print

('輸入數量不符合資料格式!'

)else

:break

order =

order[

'訂單號:'

]= i

order[

'商品名稱:'

]= goods_dict[good_index]

['name'

] order[

'單價:'

]= goods_dict[good_index]

['price'

] order[

'購買數量:'

]= good_num

print

('你的訂單如下:'

)for item in orders:

print

("訂單編號:{} \t商品名稱:{} \t 單價:{} \t購買數量:{}"

.format

(item[

'訂單號:'

], item[

'商品名稱:'

], item[

'單價:'

],item[

'購買數量:'])

) is_keep=

input

('是否繼續購物:(繼續請輸入yes)'

)if is_keep==

'yes'

: order_add(

)else

: money =

0for key in orders:

money += key[

'購買數量:'

]* key[

'單價:'

]print

('你的訂單已提交,應付金額為%d元'

%money)

pay =

0while

not pay ==money:

while1:

try:

pay =

int(

input

('請輸入付款金額:'))

except

:print

('請輸入正確付款金額!'

)else

:break

print

('請輸入正確付款金額!'

)print

('購物成功,歡迎再次光臨!'

)if __name__ ==

'__main__'

:print()

goods_info(

) isbuy =

input

('是否購買商品:(購買請輸入『yes』)'

)if isbuy ==

'yes'

: order_add(

)else

:print

('謝謝你的光顧!'

python程式設計案例 python程式設計案例

利用python內建函式和列表物件方法def demo2 x,y,z i max x ipos x.index i j max y jpos y.index j k max z kpos z.index k return i j k,ipos,jpos,kpos 但是,上面的 看上去還是很笨啊,傻傻...

Python程式設計基礎之小案例練習

re庫 re庫是python的標準庫,主要用於字串匹配。方法解釋說明 re.search 在乙個字串中搜尋匹配正規表示式的第乙個位置,返回match物件 re.match 從乙個字串的開始位置起匹配正規表示式,返回match物件 re.findall 搜尋字串,以列表型別返回全部能匹配的字串 re....

Python程式設計案例課堂

python程式設計案例教程 一 導學課 課程簡介 本課程從初學者的角度出發,以通俗易懂的語言 豐富多彩的例項,詳細地介紹了使用python 3進行程式開發所需掌握的知識和技術。本課程共分為13章,內容涵蓋 開始python程式設計之旅,python程式設計基礎知識,判斷和迴圈語句,字串的使用,列表...