【Python之旅】第二篇(三):基于列表处理的购物清单程序
1.基本需求
????編寫一個購物小程序,要求實現(xiàn)如下功能:
(1)讓用戶輸入工資;
(2)輸出購物菜單及產(chǎn)品價格;
(3)計算用戶是否可支付;
(4)輸出用戶剩余的錢,問用戶是否繼續(xù)購物,如果選擇繼續(xù),則繼續(xù)進行,否則退出程序;
(5)若錢不夠,輸出用戶還需要工作多久才能買得起(這里暫不實現(xiàn)此功能)。
2.實現(xiàn)基本思路
????基本思路可如下所示:
????在編寫程序的時候即以該思路為主線,具體細節(jié)下面再提及。
3.實現(xiàn)細節(jié)
????基于友好用戶界面的原則,實現(xiàn)的細節(jié)可總結(jié)如下:
(1)用戶輸入工資時,如果輸入的是非數(shù)字或沒有輸入,會有提示再次輸入,而不會異常退出;
(2)用戶輸入購買物品時,如果輸入的是非商品索引或非quit退出時,會有提示再次輸入,而不會異常退出;
(3)每添加商品到購物列表后,會提示用戶當前所剩余的money;
(4)用戶選擇退出時,會打印用戶的購物清單,同時輸出用戶剩余的money;
(5)總的原則是,程序在執(zhí)行過程中不會有異常情況出現(xiàn),即使用戶輸入非法字符。
4.實現(xiàn)代碼與注釋
????基于上述需求的實現(xiàn)代碼如下:
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 | #!/usr/bin/env?python import?sys market?=?[ ????['Xiaomi?Phone',?2400], ????['Iphone',?6000], ????['Computer',?3800], ????['Ipad',?1800], ????['Core?Python',?69], ????['Router',?109] ] shop_list?=?[]??? #The?shopping?list?of?the?buyer. while?True: ??salary?=?raw_input('Please?input?your?salary?per?month:').strip() ??if?not?salary.isdigit(): ????print?'Please?enter?your?salary.' ????continue ??salary?=?int(salary) ??print?"Welcome?to?our?market!You?can?buy?something?cool?here,?or?you?can?enter?'quit'?to?left.\n" ??break #Upon:input?the?salary?per?month.輸入工資同時避免異常情況。 while?True: ??print?'The?goods?we?serve?are?as?follow:' ??for?goods?in?market: ????print?market.index(goods),goods[0],goods[1] ??? ??#Upon:print?the?goods?list. ??choice?=?raw_input('What?do?you?want?to?buy?') ??? ??if?choice?==?'quit':???? #'quit'?system ????print?'\nYour?shopping?list?are?as?follow:' ????for?goods?in?shop_list: ??????print?goods[0],goods[1] ????print?'Now?you?have?%d?left.'?%?(salary) ????sys.exit('Goodbye!') ??elif?len(choice)?==?0:?? #'enter'?system ????continue #實際上'luanma'?system和'enter'?system可以只用前者進行替代,這里只是想輸出不同提示而已。 (即輸入亂碼時會提示,輸入enter時不進行提示。)? ??if?not?choice.isdigit():? #'luanma'?system,即如果用戶輸入非數(shù)字時,會有提示 ????print?'Please?input?the?right?choice.(Number?to?buy?things?and?quit?to?quit.)\n' ????continue ????? ??#Upon:?'quit'?system?,?'enter'?system?and?'luanma'?handle?system. ??? ??choice?=?int(choice) ??if?choice?>=?len(market): ????print?'Could?not?find?the?item,?try?again!\n' ????continue??? ??pri?=?market[choice] ??#Upon:To?check?if?the?number?is?legal.確認輸入的數(shù)字是否在合法范圍內(nèi) ??? ??pri?=?market[choice] ??? ??if?pri[1]?<=?salary: ????salary?=?salary?-?pri[1]??#The?remaining?money. ????shop_list.append(pri)?????#Adding?the?goods?to?the?list. ????print?'You?have?added?%s?to?your?shopping?list,?and?you?have?%d?left.\n'?%?(pri[0],?salary) ??else: ????print?'''You?have?%d?left. You?can?not?afford?to?buy?%s,?but?you?can?try?to?buy?other?things.\n'''?%?(salary,?pri[0]) |
????因為靈活用了列表作處理,所以相對來說代碼不會太復(fù)雜。
4.測試
·對輸入過程中可能出現(xiàn)的各種情況和說明如下:
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 | xpleaf@xpleaf-machine:/mnt/hgfs/Python/day2$?python?myshop.py? Please?input?your?salary?per?month: Please?enter?your?salary.????===>直接輸入'Enter'時會有提示 Please?input?your?salary?per?month:klkdf Please?enter?your?salary.????===>輸入亂碼時也會有提示 Please?input?your?salary?per?month:10000 Welcome?to?our?market!You?can?buy?something?cool?here,?or?you?can?enter?'quit'?to?left. The?goods?we?serve?are?as?follow:????===>先打印了一個商品菜單 0?Xiaomi?Phone?2400 1?Iphone?6000 2?Computer?3800 3?Ipad?1800 4?Core?Python?69 5?Router?109 What?do?you?want?to?buy?0????===>正確輸入商品索引號,會提示當前用戶購物信息 You?have?added?Xiaomi?Phone?to?your?shopping?list,?and?you?have?7600?left. The?goods?we?serve?are?as?follow: 0?Xiaomi?Phone?2400 1?Iphone?6000 2?Computer?3800 3?Ipad?1800 4?Core?Python?69 5?Router?109 What?do?you?want?to?buy?????===>只輸入'Enter'時,不會有提示輸出,但會再次打印商品菜單 The?goods?we?serve?are?as?follow: 0?Xiaomi?Phone?2400 1?Iphone?6000 2?Computer?3800 3?Ipad?1800 4?Core?Python?69 5?Router?109 What?do?you?want?to?buy?3 You?have?added?Ipad?to?your?shopping?list,?and?you?have?5800?left. The?goods?we?serve?are?as?follow: 0?Xiaomi?Phone?2400 1?Iphone?6000 2?Computer?3800 3?Ipad?1800 4?Core?Python?69 5?Router?109 What?do?you?want?to?buy?eqwer????===>輸入亂碼時,會有提示,并接受再次輸入 Please?input?the?right?choice.(Number?to?buy?things?and?quit?to?quit.) The?goods?we?serve?are?as?follow: 0?Xiaomi?Phone?2400 1?Iphone?6000 2?Computer?3800 3?Ipad?1800 4?Core?Python?69 5?Router?109 What?do?you?want?to?buy?9????===>輸入不在合法范圍內(nèi)的數(shù)字時,也會有提示 Could?not?find?the?item,?try?again! The?goods?we?serve?are?as?follow: 0?Xiaomi?Phone?2400 1?Iphone?6000 2?Computer?3800 3?Ipad?1800 4?Core?Python?69 5?Router?109 What?do?you?want?to?buy?quit????===>正常退出,會打印用戶總的購物清單信息???? Your?shopping?list?are?as?follow: Xiaomi?Phone?2400 Ipad?1800 Now?you?have?5800?left. Goodbye! |
????相對來說,用戶界面還是比較友好的,因為不會有太大的異常情況出現(xiàn),同時也實現(xiàn)了基本的需求。
本文轉(zhuǎn)自 xpleaf 51CTO博客,原文鏈接:http://blog.51cto.com/xpleaf/1693767,如需轉(zhuǎn)載請自行聯(lián)系原作者
總結(jié)
以上是生活随笔為你收集整理的【Python之旅】第二篇(三):基于列表处理的购物清单程序的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: js、jquery实用小技巧集合
- 下一篇: 主从故障处理--session 级别参数