11
我在做一個排名類型的事情,會發生什麼是我比較分數與當前分數,如果分數低於當前那麼玩家得到了高分,但是當使用這個這裏的代碼大於小於,蟒蛇
print "Score = " + str(score) + ", Compared to = " + str(array[x])
if score < array[x]:
#Do stuff here
但即使score是4,array [x]是2,if語句仍然完成?
我做錯了什麼?
我的理解是,如果score 4和array [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
最簡單的方法來檢查:'打印再版(評分),再版(陣列[X])'。另外:在Python 3中,你會得到'TypeError:無法定義的類型:int()
Dougal
2012-08-01 21:33:16
爲什麼我沒有想到那個D:我想我可能是 – FabianCook 2012-08-01 21:33:28
使用'print type(score)'它回來了''但我認爲它是這樣做的數組。 –
FabianCook
2012-08-01 21:35:11