我正在尝试创建一个本质上是带有瓶子的回购程序的函数,规则如下
金钱->客户拥有的金额
bottlesOwned->客户必须更换的瓶子数量
价格->一瓶汽水的价格
exchangeRate->汇率,以元组表示。第一个要素是可以更换的瓶子组的最小尺寸。第二个参数是一组瓶子收到的退款。
客户一次访问商店可以退回任意数量的瓶子,但是退款的瓶子总数必须是exchangeRate的第一个元素的倍数。
此功能必须输出客户在所有行程中可以购买的瓶子总数,直到客户用完为止。
<button id="play">
Downloading audio...
</button>
<br/>
<span id="duration"></span>
<br/>
<audio controls>
<source src="https://leaderfoldera5b0.blob.core.windows.net/client/Symphony%20No.6%20(1st%20movement).m4a?sv=2019-02-02&st=2020-05-24T01%3A21%3A54Z&se=2021-05-25T01%3A21%3A00Z&sr=b&sp=r&sig=NHESmCHWR23EltpmSRnmibh8cwSH%2Fn%2FU1NmI2VOzTvI%3D" type="audio/x-m4a" />
</audio>
就我所知,但是我不知道如何在不使用全局变量的情况下如何使瓶子购买计数器计时(我不能使用全局变量,因为它不会在并发测试中重置并且提供了错误的答案)
答案 0 :(得分:1)
您已经关闭。您只需要bottlesbought
作为函数的参数即可:
def lightningBottle(money, bottlesOwned, price, exchangeRate, bottlesbought=0):
if bottlesOwned >= exchangeRate[0]:
bottlesOwned -= exchangeRate[0]
money += exchangeRate[1]
if money >= price:
bottlesOwned += 1
bottlesbought += 1
money -= price
return lightningBottle(money, bottlesOwned, price, exchangeRate, bottlesbought)
else:
print ("we bought",bottlesbought,"bottles")
return bottlesbought
您可以为它提供一个默认值,因此您无需在开始时将其指定为零(始终为零)。