提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
Mid-module Assignment - Assignment 1
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