我正在尝试将程序从 Python 转换为 LSL(SecondLife 脚本)以找到我无法从函数中访问的数字。这个数字是我的菩提树余额(游戏中的钱)。我可以从脚本中确定该金额的唯一方法是向自己转帐金额并检查付款是否成功。
为了做到这一点,我找到了一个完美运行的二分法 python 脚本:
from random import randint
n = 100000000 # Maximum amount possible
amountToFind = randint(1,n)
a = 1
b = n
nbAnswers = 0
while b-a >=0:
answer= (a+b)//2
nbAnswers += 1
if answer== amountToFind:
print("The correct answer is:", answer)
break
elif answer> amountToFind:
b = answer- 1
else:
a = answer+ 1
print("The number of steps to get the correct amount was :", nbAnswers)
print(amountToFind)
问题是我无法与answer
我正在寻找的数字进行比较。我只有失败和成功:
llCheckLindenBalance()
{
rep = (a+b)/2;
transactionId = llTransferLindenDollars(owner, rep);
}
touch_start(integer total_number)
{
llCheckLindenBalance();
}
transaction_result(key id, integer success, string data)
{
if(id != transactionId)
return;
if(success) // rep < amount or rep == amount
{
a = rep+1;
llOwnerSay("Last amount " +(string)rep);
llCheckLindenBalance();
}
else // FAIL - rep < amount
{
b = rep-1;
llCheckLindenBalance();
}
}
该脚本到目前为止有效,但它永远不会停止,因为它永远不知道何时停止。我正在考虑比较a
,b
但它们之间的空间也是可变的(从 0 到 10)。所以我坚持下去。我考虑过测试answer+1
,如果失败,则意味着它是最高数量,但我现在无法在哪里测试它。
你们有什么想法吗?