世界:计数器有问题



它在每次猜测之后打印单词,而不是在6次猜测结束后给他们单词

我尝试设置attempts = 6如果这个单词在我的json文件中的单词列表中它会从尝试中减去1如果这个单词不在json文件中它不会从尝试中减去如果尝试达到零它会跳出循环并给他们单词

import json
import random
black = '33[40m'
green = '33[42m'
yellow = '33[43m'
f = open('wordle_no_dupes.json')
info = json.load(f)
f.close
word = random.choice(info)
print("Enter a 5 letter word: ")
attempts = 6
for attempt in range(1, 7):
guess = (input("Enter Guess: ").lower())
if guess in info:
attempts = attempts - 1   
if guess not in info:
attempts = attempts - 0
if attempts == 0:
break
print("The word was", word)
for i in range(5):
if guess[i] == word[i]:
print(green, guess[i] , end = "")
elif guess[i] in word:
print(yellow, guess[i] , end = "")
else:
print(black, guess[i] , end = "")
if guess == word:
break
print("You got it!!")

问题是您在for循环中包含了print()语句。创建一个新变量并存储答案是否正确是个好主意。例如:

attempts = 6
answerCorrect = false
for attempt in range(1, 7):
guess = (input("Enter Guess: ").lower())
if guess in info:
attempts = attempts - 1   
if guess not in info:
attempts = attempts - 0
if attempts == 0:
break
for i in range(5):
if guess[i] == word[i]:
print(green, guess[i] , end = "")
elif guess[i] in word:
print(yellow, guess[i] , end = "")
else:
print(black, guess[i] , end = "")
if guess == word:
answerCorrect = true
break
if answerCorrect == false: // Use this if statement to determine which line to print
print("The word was", word)
else:
print("You got it!!")

不要忘记将answerCorrect设置为true!只包括代码的最后一部分,因为在此之前我没有更改任何内容。希望这对你有帮助!

最新更新