我正在做一个排名类型的事情,发生的事情是我将得分与当前得分进行比较,如果得分低于当前那么玩家获得了高分,但是在这里使用此代码时
print "Score = " + str(score) + ", Compared to = " + str(array[x])
if score < array[x]:
#Do stuff here
但即使得分为4且数组[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
如果得分= 4则打印出来,并且数组由[1,2]
组成Score = 4, Compared to = 1
Set rank to: 0
Score = 4, Compared to = 2
Rank = 0
答案 0 :(得分:23)
检查以确保得分和数组[x]都是数字类型。您可能正在将整数与字符串进行比较......这在Python 2.x中是令人心碎的。
>>> 2 < "2"
True
>>> 2 > "2"
False
>>> 2 == "2"
False
修改