使用 Python 2.7。
我希望能够输入一个数字,并返回完成等式 win_percentage = observed 的所有 x 和 y 值集。
def Rlog5(observed):
z = float(observed)/1000
for x in range(350, 650, 1):
y = 1000 - x
win_percentage = (float(x)-(float(x)*float(y)))/(float(x)+float(y)-(2*(float(x)*float(y))))
if win_percentage = observed:
print (z, float(x), float(y))
当我运行该函数时,我什么也没得到。没有错误,但也没有值(我尝试过使用和不使用 x 的 float ,但我认为它需要它们,因为 win_percentage 应该是一个 float )。最令人沮丧的部分是我有这段代码,它基本上做同样的事情,而且工作正常:
def solve(numNuggets):
for numTwenties in range(0, numNuggets/20 + 1):
for numNines in range(0, (numNuggets - numTwenties*20)/9 + 1):
numSixes = (numNuggets - numTwenties*20 - numNines*9)/6
totNuggets = 20*numTwenties + 9*numNines + 6*numSixes
if totNuggets == numNuggets:
print (numSixes, numNines, numTwenties)
我知道这种新手问题,但我无能为力......
最佳答案
正如其他人所指出的,您有 =
而您应该有 ==
,但我认为这是一个错字,因为您会遇到语法错误。但是您正在测试 float 是否相等,而 float 由于不精确而几乎永远不会相等。通常,您会测试两个 float 是否在彼此之间的微小差异内,传统上称为 epsilon
。
试试这个:
if abs(win_percentage - observed) < 0.000001:
print etc
关于python - 在 python 中迭代时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7465824/