2014-10-26 61 views
-1

在猜出正確之後,似乎無法打印出「恭喜,您是正確的」。Python-無法打印您贏得

從隨機導入randint

guessesTaken = 0

randomNumber = [STR(randint(1,9))爲_在範圍(4)]#創建隨機NUMS的列表

guessesTaken < 10:

guesses = list(input("Guess Number: ")) # create list of four digits 
check = "".join([b if a==b else "H" if int(a)>int(b) else "L" for a, b in zip(guesses,randomNumber)]) 
if all(c == 'Y' for c in check): # if check has four Y's we have a correct guess 
    print("Congratulations, you are correct, it took you", guessesTaken, "guesses.") 
    break 
else: 
    guessesTaken += 1 # else increment guess count and ask again 
    print(check) 
if guessesTaken == 10: 
    print("You lose") 
+2

'check == randomNumber' ** not ** test *「if check has four Y's」* * – jonrsharpe 2014-10-26 18:11:12

回答

0

這裏發生的事情是因爲check是字符串,randomNumber是一個列表,比較他們將永遠返回False。您需要更改其中一個類型以按照您的方式進行比較。將if聲明更改爲:

if check == ''.join(randomNumber):