大于小于,python [英] Greater than less than, python
问题描述
我正在做排名类型的事情,发生的事是我将分数与当前分数进行比较,如果分数低于当前分数,则玩家获得了较高的分数,但是在此处使用此代码
I am doing a ranking type thing, what happens is I compare the score to the current score and if the score is lower then the current then the player has got a high score, but when using this code here
print "Score = " + str(score) + ", Compared to = " + str(array[x])
if score < array[x]:
#Do stuff here
但是,即使分数是4且array [x]是2,if语句仍然完成吗?
But even if score is 4 and array[x] is 2 the if statement is still done?
我做错什么了吗?
我的理解是,如果分数4和array [x]为2,那么4大于2,这意味着它返回False?
My understanding is that if score 4 and array[x] is 2 then 4 is greater than 2 which means it comes back 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]组成,则会打印此内容
it prints this if score = 4 and the array is made up of [1, 2]
Score = 4, Compared to = 1
Set rank to: 0
Score = 4, Compared to = 2
Rank = 0
推荐答案
检查以确保score和array [x]均为数字类型.您可能正在将整数与字符串进行比较...这在Python 2.x中是令人心碎的.
Check to make sure that both score and array[x] are numerical types. You might be comparing an integer to a string...which is heartbreakingly possible in Python 2.x.
>>> 2 < "2"
True
>>> 2 > "2"
False
>>> 2 == "2"
False
编辑
进一步的解释: Python如何比较字符串和整数?
这篇关于大于小于,python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!