我在學校的空閒時間正在編寫一個Hangman程序,作爲Python的入門級項目,我終於遇到了障礙。在字符串中存儲字符的位置
我想做什麼:
到目前爲止,我已經印刷電路板,以輸入的字的大小,我可以決定,如果猜測正確與否,用工作成績。現在我需要做出正確的猜測,並將str(board)中正確的下劃線改爲它們的實際字母。要做到這一點,我想抓住字符在FULL CORRECT WORD中被猜測的位置(如果正確),然後簡單地將該位置改爲正確的字母。對不起,羅嗦!這是我的代碼。
print("Welcome to Tyler's Hangman game!!!! Player one, input a word for player two to guess, lowercase!\n")
playAgain = True
guessing = True
#assure game starts in the True state
while playAgain == True:
word = str(raw_input())
wordLength = int(len(word))
board = "_ " * wordLength
lives = 9
#initialize/reset variables on a new play
while guessing == True:
print("\n" * 15)
print("Now its player two's turn to make a guess! You have 9 lives! Only one letter at a time, lowercase!\n")
print(board + "Lives: " + str(lives) + "\n")
#print board + lives
guess = str(raw_input())
if guess in word:
print("Correct!")
word.index(guess)
else:
print("Wrong.")
score -= 1
#check guess and change board
以單詞猜測開始的說法:事情開始讓我感到困惑。謝謝!!
摘錄我很困惑與
if guess in word:
print("Correct!")
word.index(guess)
什麼讓你困惑。你能解釋哪些行爲不像你期望的那樣? – idjaw
'如果在單詞猜測中檢查字符串'單詞'中的子串'guess'。如果爲true,則打印出「正確」,然後打印出第一個出現在「猜測」處的索引。當然,「print(word.index(guess))'更清晰。 –
啊我想我不明白索引做了什麼。我不想打印索引,我只想檢查該字符出現在哪些點上,然後我想在程序中稍後使用這些整數 –