Python中的If语句错误

Python中的If语句错误,python,Python,我的Python代码中似乎有一个反复出现的错误。。。我想问题在if语句中,您是否有可能帮助我?我在学校做作业,如果用户猜到了这个词,他们会得到祝贺,如果没有,他们必须再试一次 我的代码: import time import random import os words = open("Words.txt","r") WordList = [] for lines in words: WordList.append(lines) WordList=[line.rstrip('\n')f

我的Python代码中似乎有一个反复出现的错误。。。我想问题在if语句中,您是否有可能帮助我?我在学校做作业,如果用户猜到了这个词,他们会得到祝贺,如果没有,他们必须再试一次

我的代码:

import time
import random
import os

words = open("Words.txt","r")
WordList = []

for lines in words:

 WordList.append(lines)
 WordList=[line.rstrip('\n')for line in WordList]


print(WordList[0:3])
print(WordList[3:6])
print(WordList[6:9])
time.sleep(2)
os.system(['clear','cls'][os.name == 'nt'])

random.shuffle(WordList)

print(WordList[0:3])
print(WordList[3:6])
print(WordList[6:9])

removedword = (WordList[9])

print("---------------------------")

guesses = 0

while guesses <3:
guess = input("What is the removed word?")
guesses = guesses + 1

  if guess == removedword:
      print("You have guessed correctly!")

  else:
      print("Fail")

您的while循环计数为3,即使您的答案正确,也不会停止。为了避免这种情况,您需要检查答案是否正确,并打破循环

这是修改后的代码:

import time
import random
import os

words = open("Words.txt","r")
WordList = []

for lines in words:
    WordList.append(lines)
    WordList=[line.rstrip('\n')for line in WordList]


print(WordList[0:3])
print(WordList[3:6])
print(WordList[6:9])
time.sleep(2)
os.system(['clear','cls'][os.name == 'nt'])

random.shuffle(WordList)

print(WordList[0:3])
print(WordList[3:6])
print(WordList[6:9])

removedword = (WordList[9])
#printed this so I could win every time
#print(removedword)

print("---------------------------")

guesses = 0
#Added flag 
unanswered = True

#while guesses less than 3 and question is unanswered 
while guesses <3 and unanswered:
    guess = input("What is the removed word?")
    guesses = guesses + 1
    if guess == removedword:
        print("You have guessed correctly!")
        #correct answer, changes flag
        unanswered = False
    else:
        print("Fail")

if unanswered:
    print("Game over!")

你犯了什么错误?你能提供一个回溯吗?你能正确地安排你的代码并用错误细节提问吗。代码和你刚才描述的完全一样。-根据设计,既然你说这是一个错误,你希望它做什么?错误是当我输入正确的答案时,它会再次问我答案。我现在已经设置了它,如果他们得到正确的答案,它会自动将“猜测”变量设置为3。当用户猜测正确时,你只需要暂停。它将退出while循环。它将打印失败3次,而不是第三次加载游戏
guesses = 0
flag = 0
while guesses <3:
 guess = input("What is the removed word?")
 guesses = guesses + 1

 if guess == removedword:
     print("You have guessed correctly!")
     flag = 1
     break
  else:
     print("Fail")

if flag == 0:
  print("Game over")
guesses = 0

while guesses <3:
    guess = input("What is the removed word?")
    guesses = guesses + 1

    if guess == removedword:
      print("You have guessed correctly!")
      break
    else:
      if guesses == 3:
        print("Game over")
      else:
        print("Try again")
import time
import random
import os

words = open("Words.txt","r")
WordList = []

for lines in words:
    WordList.append(lines)
    WordList=[line.rstrip('\n')for line in WordList]


print(WordList[0:3])
print(WordList[3:6])
print(WordList[6:9])
time.sleep(2)
os.system(['clear','cls'][os.name == 'nt'])

random.shuffle(WordList)

print(WordList[0:3])
print(WordList[3:6])
print(WordList[6:9])

removedword = (WordList[9])
#printed this so I could win every time
#print(removedword)

print("---------------------------")

guesses = 0
#Added flag 
unanswered = True

#while guesses less than 3 and question is unanswered 
while guesses <3 and unanswered:
    guess = input("What is the removed word?")
    guesses = guesses + 1
    if guess == removedword:
        print("You have guessed correctly!")
        #correct answer, changes flag
        unanswered = False
    else:
        print("Fail")

if unanswered:
    print("Game over!")