在這個例子中,我們將設置PH[0]
到'Ten of Hearts'
,並guess
到'Ten'
「雖然不是」錯誤 - 蟒蛇
名單「PH」表示去魚的紙牌遊戲玩家的手。當用戶猜測一張卡片時,他們必須猜出一張與他們手中的卡片相對應的卡片。我寫了這個代碼塊,以便如果用戶輸入一個無效猜測(如果猜測不在PH
,他們會再次提示輸入一個新的猜測)
我覺得我有問題的變量在數組[guess1, guess2, guess3, guess4]
,但我不太確定。
執行代碼時,循環會一直持續。我需要能夠退出循環以便返回猜測,以便它可以輸入到下一個函數中。
如果有人能幫我解決我遇到的問題。
guess = str(raw_input("Make a guess : "))
guess11 = guess, 'of Hearts'
guess1 = " ".join(guess11)
guess22 = guess, 'of Diamonds'
guess2 = " ".join(guess22)
guess33 = guess, 'of Clubs'
guess3 = " ".join(guess33)
guess44 = guess, 'of Spades'
guess4 = " ".join(guess44)
while PH[0] not in [guess1, guess2, guess3, guess4] :
print "You do not have a card like that in your hand."
guess = str(raw_input("Make another guess : "))
guess11 = guess, 'of Hearts'
guess1 = " ".join(guess11)
guess22 = guess, 'of Diamonds'
guess2 = " ".join(guess22)
guess33 = guess, 'of Clubs'
guess3 = " ".join(guess33)
guess44 = guess, 'of Spades'
guess4 = " ".join(guess44)
return guess
該代碼傷害了我的眼睛!瞭解如何撰寫[DRY](http://en.wikipedia.org/wiki/Don't_repeat_yourself)代碼。它會讓你的生活變得更容易。 – Basic 2014-09-27 23:17:01
當我將'PH [0]'設置爲''''十'心'和'猜'到''''時,它對我有用。該循環正確退出。 – interjay 2014-09-27 23:18:17
'raw_input'已經是一個字符串 – 2014-09-27 23:20:41