【发布时间】:2020-05-24 02:13:26
【问题描述】:
我正在尝试创建一个本质上是一个带有瓶子的回购程序的功能,规则如下
money -> 客户拥有的金额
bottlesOwned -> 客户必须交换的瓶子数量
price -> 一瓶汽水的价格
exchangeRate -> 汇率,以元组表示。第一个元素是可以交换的瓶子组的最小尺寸。第二个参数是一组瓶子收到的退款。
客户可以在一次访问商店时退还任意数量的瓶子,但退款的瓶子总数必须是 exchangeRate 第一个元素的倍数。
该函数必须输出客户在所有行程中能够购买的瓶子总数,直到客户用完钱为止。
def lightningBottle(money, bottlesOwned, price, exchangeRate):
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)
else:
print ("we bought",bottlesbought,"bottles")
return bottlesbought
据我所知,但我无法弄清楚如何在不使用全局变量的情况下让瓶子购买计数器打勾(我不能使用全局变量,因为它不会在并发测试时重置并提供错误答案)
【问题讨论】:
标签: python python-3.x recursion counter