我正在做一个排名类型的事情,发生的情况是我将分数与当前分数进行比较,如果分数低于当前分数,那么玩家获得了高分,但是在这里使用此代码时
print "Score = " + str(score) + ", Compared to = " + str(array[x])
if score < array[x]:
#Do stuff here
但是即使 score 是 4 并且 array[x] 是 2 if 语句仍然完成?
难道我做错了什么?
我的理解是,如果得分 4 和数组 [x] 为 2,那么 4 大于 2,这意味着它返回 False?
这是完整的代码
def getRank(array, score):
rank = 0
rankSet = False
for x in range(0, len(array)):
print "Score = " + str(score) + ", Compared to = " + str(array[x])
if score < array[x]:
if not rankSet:
rank = x
print "Set rank to: " + str(rank)
rankSet = True
elif score == array[x] or score > array[x]:
rank += 1
print "Rank higher than " + str(x)
print "Rank = " + str(rank)
return rank
如果 score = 4 并且数组由 [1, 2] 组成,它会打印这个
Score = 4, Compared to = 1
Set rank to: 0
Score = 4, Compared to = 2
Rank = 0