iT邦幫忙

第 11 屆 iThome 鐵人賽

DAY 20
0
Software Development

從0開始學習程式-Python系列 第 21

[Day24]一起來做飲料點餐系統吧!

突然想到我們在Day22的題目解答還沒release,那我們今天就當成程式範例時間吧!

bonus=5 #全班總成績的bonus
from functools import reduce
def accum(grade,weight):
    bonus=3
    A=map(lambda x,y:x*y,grade,weight)
    return bonus+reduce(lambda x,y:x+y,A)

if __name__=='__main__':
    grade=[85,83,70,88,78]
    w=[0.05,0.2,0.3,0.35,0.1]
    print('Hank got {} marks in this course.'.format(bonus+accum(grade,w))) 

#output:Hank got 88.45 marks in this course.

如果想要製作一個簡易的飲料店點餐機呢?

現在系週是個常見的系上活動,炎熱的夏天賣個飲料應該很好賺吧!假設我們只賣紅茶、綠茶、奶茶,也有中杯和大杯的選擇,計價規則依照數字嵐飲料店的規則,並且攜帶飲料杯可以折價,另外塑膠袋需要收費,則你會怎麼做呢?
在連結提供完整code給大家參考。

大概在做這個設計時,我們遵守幾個原則:

  1. 盡可能將程式分類並架構化,方便針對後續修改時比較友善
  2. 使用dict來做找查,降低運算時間
  3. 變數命名盡可能使用可讀性比較高的命名方法
  4. 有除錯(輸入錯誤)的判斷及控制方法

下面...的地方就是因為程式太長了,這裡省略可能要麻煩大家到這裡查看。

import math

#------------Basic questions and identifications-----------
def set1():
	tyPe={"a":"紅茶","b":"綠茶","c":"奶茶"}
	size={"a":"中杯","b":"大杯"}
	Babo={"a":"加珍珠","b":""}
	sugar={"a":"全糖","b":"少糖","c":"無糖"}
	ice={"a":"全冰","b":"半冰","c":"去冰"}
	more={"a":True,"b":False}
	return [tyPe,size,Babo,sugar,ice,more]

def problems():
	Q0="請問你要什麼飲料:a:紅茶,b:綠茶,c:奶茶"
	Q1="請問你要的大小是:a:中杯,b:大杯"
	Q2="請問你要加珍珠嗎:a:加,b:不加"
	Q3="請問你的甜度是:a:全糖,b:少糖,c:無糖"
	Q4="請問你的冰塊是:a:全冰,b:半冰,c:少冰"
    ...
	return[Q0,Q1,Q2,Q3,Q4,..]

#------------Main qusestion-----------
#------------Using k confirm program working-----------

def ask():
	k=True # 判斷要不要繼續問下去的參數
	Set=set1()
	question=problems()
	current_order=[]

#------------What drink do you want?---------
	for i in range(5):
		print(question[i])
		check=input()
		if check in Set[i]:
			current_order.append(Set[i][check])
		else:#當輸入引述不在dict中時,回傳False
			k=False

#------------How many do you need?---------
	print(question[5])
	check=input()
	if check.isdigit(): #判斷是否為數字
		current_order.append(int(check))
	else:#當輸入引述不在dict中時,回傳False
		k=False

#------------Would you want more?---------
	print(question[6])
	check=input()
	if (check in Set[5]):
		order_more=Set[5][check]#當要繼續點餐order_more=True,不點餐order_more=False
	else:
		order_more=False
		k=False
	return current_order,k,order_more #ask customers what he want

#------------Final question(bags, cups)------------

def recy(cup): #is the discount of the cup due to environment friendly
...

def bag(cup): #is the cost of the bag
...


#------------Caculate cost------------
def Price(drink,babo_check):
...

#------------Classification and statistics------------
def classification(drinklist): 
    #This is classification and statistics of various beverages.
	cup,total_drink_price=0,0
	collection={}
	
	for i in range(len(drinklist)):
		beverages_mainname=drinklist[i][0]+drinklist[i][1] # type of drink
		icesur=drinklist[i][2]+drinklist[i][3]+drinklist[i][4] # ice and sugar
		
		# caculating price just drink
		total_drink_price+=Price(beverages_mainname,drinklist[i][2])*drinklist[i][5] 
		# caculating total cups
		cup+=drinklist[i][5] 
		
		#classification and counting
		if (beverages_mainname+icesur in collection):
            collection[beverages_mainname+icesur]=collection[beverages_mainname+icesur]
                                                                       +drinklist[i][5]
		elif (not (beverages_mainname+icesur in collection)) and drinklist[i][5]!=0:
			collection.setdefault(beverages_mainname+icesur,drinklist[i][5])
	return collection,cup,total_drink_price


#------------This is main.------------
...

#------------Bags and discounts------------
...

今天就先到這吧!
希望這一個比較大的題目可以讓大家知道,未來可能在製作一些程式可能不會只有數學問題,當面臨處理一般民生生活也相當重要的!


上一篇
[Day23]OMG!OMG! 跨檔案的global variable不受控!
下一篇
[Day25]Object&Class到底是什麼?
系列文
從0開始學習程式-Python32
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言