Wordle: Having issues with counter
Question:
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 = ' 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!!")
Answers:
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!
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 = ' 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!!")
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!