Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Wordel: Having issues with counter

It prints the word after every guess instead of giving it to them after the 6 guesses are over

I tried setting attempts = 6 and if the word was in the list of words in my json file it would subtract one from attempts and if the word guessed wasn’t in the json file it wouldn’t subtract from attempts and if attempts reached zero it would break out of the loop and give them the word

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!!")
        

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

The problem is that you’re including the print() statement in the for loop. It would be a good idea to create a new variable and store whether the answer is correct.
For example:

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!!")

Don’t forget to set answerCorrect to true! Only including the last section of the code as I didn’t change anything before this. Hope this helps!

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading