[英]List index while loop python
我有以下代碼的問題。 當我輸入確切的密碼字符時,以下代碼與密碼不匹配。
#initiate words for guessing secretWords =['cat','mouse','donkey','ant','lion']
#Generate a random word to be guessed generateWord = (random.choice(secretWords))
# User have attempts only to the random generate words LeftCount = 6
generateWord = ["_"] * len(secretWords) userInput="" LetterNumber=0 RightGuess =0
while (LeftCount !=0):
print ("Word Contains", len(generateWord),"letters")
print ("You have", str(LeftCount),"attempts remaining")
print ("\n")
print(generateWord)
print ("\n")
userInput=input("Enter word: ")
while(LetterNumber< len(generateWord)):
if(generateWord[LetterNumber] == userInput):
generateWord[LetterNumber]= userInput
RightGuess +=1
LetterNumber +=1
LeftCount -=1
LetterNumber=0
if (RightGuess == len(generateWord)):
print ("Congratulations")
break
if(LeftCount ==0):
print ("Game over")
為什么要將 generateWord 中的一個字母與整個 userInput 進行比較?
if(generateWord[LetterNumber] == userInput):
該行將“LetterNumber”索引處的字符與 userInput 進行比較,因此如果用戶輸入一個單詞,它將永遠不會返回 true。
如果您試圖計算用戶猜測中正確字母的數量,您不應該將用戶輸入中的每個字母與“generateWord”中的相應字母進行比較。
if(generateWord[LetterNumber] == userInput[LetterNumber]):
還有一些一般性的觀點,變量名不應該以大寫開頭,根據 Python 標准應該是“letter_number”。 嘗試改進您的變量名稱,也許稱之為“generated_word”,而不是“generate_word”。 “Generate_word”暗示它是一個函數,因為 generate 是一個動詞。
if 語句之后的那一行也將整個 userInput 重新分配到 generateWord 值中,在字母索引處,你為什么要這樣做?
最后,您需要在 while 循環的結尾或開頭生成一個新單詞,因為此時您只在開頭生成一個單詞,然后它將在每次迭代中使用相同的單詞。
嘗試使用 print 打印出您的一些變量,它會幫助您調試程序,因為它絕對不是您期望的那樣。
您正在覆蓋您選擇的單詞。 generateWord
同時是秘密詞,也是用戶輸入。 這是行不通的。 這是應該做你想做的事情(我還糾正了一些其他問題):
import random
secretWords = ["cat", "mouse", "donkey", "ant", "lion"]
generatedWord = random.choice(secretWords)
leftCount = 6
userWord = ["_"] * len(generatedWord)
refusedLetters = ""
#returns all positions of character ch in the string s
def findOccurences(s, ch):
return [i for i, letter in enumerate(s) if letter == ch]
print("Word contains", len(generatedWord), "letters")
while(leftCount > 0 and generatedWord != "".join(userWord)):
print ("\n")
print ("You have", str(leftCount), "attempts remaining")
print ("Letters not present in your word:", "".join(sorted(refusedLetters)))
print ("Your word so far: ","".join(userWord))
print ("\n")
#checks that the user enters something
userInput = ""
while not len(userInput):
userInput=input("Enter letter or word: ")
userInput = userInput.lower()
#if len > 1, then the user has tried to guess the whole word:
if len(userInput) > 1:
if generatedWord == userInput:
print("Congratulations")
break
else:
print("Wrong word, sorry")
#len == 1, thus the user asked for one letter
else:
#if letter isn't already found
if not userInput in userWord:
#for all occurences of the letter in the secret word
occurences = findOccurences(generatedWord, userInput)
for occurence in occurences:
userWord[occurence] = userInput
#if the letter was not found
if not occurences:
#if the letter has already been proposed
if userInput in refusedLetters:
print("You have already tried this letter")
continue
else:
refusedLetters += userInput
else:
print("You have already tried this letter")
continue
leftCount -= 1
#the else statement will only be entered if we did not exit the previous loop via a break.
#Thus, if the word has already been found, nothing happens here.
else:
if generatedWord == "".join(userWord):
print("Congratulations")
else:
print("Game over")
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.