【MATH6005-Introduction to Python and MATH6181-Python & Forecasting】

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档


TASK1

White a function, using the prototype provided, to make change for money from £0 to £5. The function should output the number of coins from each denomination used to make the change, i.e., £2, £1, 50p, 20p, 10p, 5p, 2p, 1p


Function prototype

def changeMarker ( value ):
	’’’
	function description
	’’’
	return two_pound , one_pound , p50 , p20 , p10 , p5 , p2 , p1

Function Behavior/atributes

• Your function must check if the value is within the range (0, 5). If not, the function must return −1 for all quantities of coins.
• Your function must give the least amount of coins for a value.

Examples of inputs and outputs

value = 6 # if the value is set equal to 6
output = changeMarker ( value )
print ( output )
# the outout should be output = (-1, -1, -1, -1, -1, -1, -1, -1)
output = changeMarker ( 2. 53 )
print ( output )
# the output should be output = (1, 0, 1, 0, 0, 0, 1, 1)

Answer

代码如下(示例):

def changeMarker(value):
    '''
    function description 
    '''
    if value<=0 or value>=5:
        two_pound, one_pound, p50, p20, p10, p5, p2, p1 = -1, -1, -1, -1, -1, -1, -1, -1
    else:
        value = value*100
        t = [200,100,50,20,10,5,2,1]
        m = [0 for _ in range(len(t))]
        for i, money in enumerate(t):
            m[i] = int(value // money)
            value = round(value%money)
            two_pound, one_pound, p50, p20, p10, p5, p2, p1 = m
            
    
    return two_pound, one_pound, p50, p20, p10, p5, p2, p1

Test

请添加图片描述
请添加图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值