python 周末大作业之2
要求:
# 1. 用戶先給自己的賬戶充錢:比如先充3000元。
# 2. 頁面顯示 序號 + 名稱 + 價格 , 如:
# # [===========有如下商品供您選擇:===========]
# # 序號 名稱 價格
# # 1 電腦 1999
# # 2 鼠標 10
# # 3 游艇 20
# # 4 美女 998
# # n或N 購物車結算
# q或Q 退出程序(如不結算購物車可直接退出)]
# [==========================================]
# 購物車結算
# 3. 用戶輸入選擇的商品序號,然后打印商品名稱及商品價格,并將此商品,添加到購物車,用戶還可繼續添加商品。
# 4. 如果用戶輸入的商品序號有誤,則提示輸入有誤,并重新輸入。
# 5. (1)用戶輸入n為購物車結算,依次顯示用戶購物車里面的商品,數量及單價
# (2)若充值的錢數不足則讓用戶刪除某商品,直至可以購買,若充值的錢數充足,則可以直接購買退出
# (3)退出程序之后,依次顯示用戶購買的商品,數量,單價,以及此次共消費多少錢,賬戶余額多少
# 6. 用戶輸入Q或者q 直接退出程序。
可以分模塊就行寫,
# 商品列表
?
goods_list = [{"name": "電腦", 'price': 1999},{"name": "鼠標", 'price': 10},{"name": "游艇", 'price': 20},{"name": "美女", 'price': 998},{"name": "油精", 'price': 30}, ]?
?
?
# 初始金額
money = 0?
# 購物車(你要求的購物車,大致呈現是什么樣子的)
car = {} ''' car = {1: {"name": "電腦", 'price': 1999},2: {"name": "鼠標", 'price': 10},3: {"name": "游艇", 'price': 20},4: {"name": "美女", 'price': 998},5: {"name": "油精", 'price': 30}, } '''?
# 充值
def chongzhi():global moneywhile 1:money1 = input('請輸入您要充值的金額:').strip()if money1.isdigit():money1 = int(money1)money += money1print('充值成功,本次充值金額為%s元,總金額%d元' % (money1, money))breakelse:print('充值失敗,請輸入數字')?
# 展示商品
def show_goods():print('序號 名稱 價格')for index, dic in enumerate(goods_list, start=1):print(index, dic['name'].rjust(6), str(dic['price']).rjust(5))?
#添加到購物車
def shopping(sn):# 這條已存在購物車里面if sn in car:car[sn]['amount'] += 1# 不存在則創建一條數據else:car[sn] = {'name': goods_list[sn - 1]['name'],'price': goods_list[sn - 1]['price'],'amount': 1}print('添加購物車成功')print('序號 名稱 單價 數量 總價')print(sn, car[sn]['name'].rjust(6), str(car[sn]['price']).rjust(5), str(car[sn]['amount']).rjust(4),str(car[sn]['price'] * car[sn]['amount']).rjust(6))?
#展示購物車
def show_car():total = 0print('[===================購物車明細如下===================]')print('序號 名稱 單價 數量 總價')for k, dic in car.items():print(k, dic['name'].rjust(6), str(dic['price']).rjust(5), str(dic['amount']).rjust(4),str(dic['price'] * dic['amount']).rjust(6))total += dic['price'] * dic['amount']return total?
# 展示購買的商品
def show_shopping():print('[===================您購買的商品明細如下===================]')print('序號 名稱 單價 數量 總價')for k, dic in car.items():print(k, dic['name'].rjust(6), str(dic['price']).rjust(5), str(dic['amount']).rjust(4),str(dic['price'] * dic['amount']).rjust(6))print('購物成功!')?
# 退出
def quit():global flagprint('[===================歡迎下次光臨===================]')flag = False?
# 刪除商品
def del_goods(sn):if sn.isdigit():sn = int(sn)if 0 < sn <= len(goods_list):car[sn]['amount'] -= 1if car[sn]['amount'] == 0:car.pop(sn)else:error2()else:error1()?
# 結算
def jiesuan():global flagwhile 1:total = show_car()print(' 您的當前金額為%d元, 您購物車的商品總價格為%d元' % (money, total))# 錢夠,可以買單if money - total >= 0:res = input('確認購買請按y,按q結束程序,按任意鍵繼續購物').strip()if res.upper() == 'Y':print('購買成功, 您剩余%d元' % (money - total))show_shopping()quit()breakelif res.upper() == 'Q':quit()flag = Falsebreakelse:break# 錢不夠,刪除掉一些商品else:res = input('您的余額不夠, 請刪除一些商品d或者充值c')if res.upper() == 'D':sn = input('請輸入您要刪除的商品序號: ').strip()# 刪除商品 del_goods(sn)elif res.upper() == 'C':chongzhi()?
# 一些報錯函數
def error1():print('您輸入的選項不存在')def error2():print('您輸入的選項超出范圍')?
# 程序入口
if __name__ == '__main__':print('[===================歡迎光臨**購物中心===================]')# 1. 充值 chongzhi()# 2. 展示商品 show_goods()# 3. 開始購物flag = Truewhile flag:sn = input('請輸入您要購買的商品序號:(按n結算,按q退出)').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()?
?小結:
1.全局變量flag在while flag 中的使用,按照之前的寫法我會把while True:寫成這樣子之后,假如遇到循環體中
(1)若不能滿足某種條件需要退出循環,就沒有辦法做到,寫個變量的話,就很好控制循環的出入,flag = 0,退出。
如:
flag = 1
while flag:
? ? if(i<5):
? ? ? ? flag=0
當執行完條件if之后,就會跳出循環。
(2)第二種情況
flag = 1
while flag:
? ? for i 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.本題程序,購物車的構造,以及商品列表的構造都是需要注意的。
?
轉載于:https://www.cnblogs.com/longerandergou/p/10927660.html
總結
以上是生活随笔為你收集整理的python 周末大作业之2的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 数据结构——数据结构中的数据表示
- 下一篇: 大专读完大一第一学期,贷款助学但是钱被自