我正在嘗試編寫一個程序,我可以在其中輸入一個列表,如果列表中的所有值相等,它將返回布爾型Yahtzee爲True。當我運行它時,它可以在99%的時間內運行。但是,索引3可能與列表中的其他值不同,並且isYahtzee()
仍然會將Yahtzee返回爲True。有人可以幫我調試嗎?檢查列表中的值是否相等返回錯誤結果
#Determine whether all of them are the same and returns a Boolean.
def isYahtzee(aList):
#This tells the program to look down the entire length of the list
for i in range (0,len(aList)):
#Since I will be working with a list with no more than 5 numbers, I se
#This equal to 5 indexs (0-4)
if aList[0] == aList[1] and aList[1] and aList[2] and aList[3] and aList[4]:
#If all of them are equal, the boolean Yahtzee = True
Yahtzee = True
#If they are not all equal to eachother
if aList[0] != aList[i]:
#The Boolean Yahtzee = False
Yahtzee = False
#Return the value of the Boolean
return Yahtzee