2016-09-30 63 views
-2

我遇到以下代码的问题。当我输入确切的密码字符时,以下代码不会与密码匹配。列表索引while循环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") 

回答

0

您正在覆盖您选择的单词。 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") 
+0

没有退出循环 – user244428

+0

确实,我没有测试过它。问题在于userWord是一个数组,而不是一个字符串。它现在变成一个字符串,只要需要。此代码现在可用。 – Efferalgan

+0

谢谢你的工作 – user244428

2

你为什么要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打印出一些变量,它会帮助你调试你的程序,因为它绝对没有达到你期望的程度。