【问题标题】:Wordle: Having issues with counterWordel:计数器有问题
【发布时间】:2022-12-06 11:59:52
【问题描述】:

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

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

import json
import random
black = '\033[40m'
green = '\033[42m'
yellow = '\033[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!!")
        

【问题讨论】:

    标签: python


    【解决方案1】:

    问题是您在 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!只包括代码的最后一部分,因为在此之前我没有做任何更改。希望这可以帮助!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-11-23
      • 1970-01-01
      • 1970-01-01
      • 2012-10-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多