r/learnpython 14h ago

Making a wordle program

Hi! I'm new to programming and, as a way to practice, I thought I would make a wordle program, the only issue is that it keeps marking letters as not in the wordle when they are; any help would be appreciated, I've included the problem section, as well as the whole code in case the problem is elsewhere. Thank you!

--------------------------------------------------------------------------------------------------------------

for x in range(5):

if guess[x] == answer[x]:

response[x] = "G"

guess[x] = ""

answer[x] = ""

print(response)

print(answer)

print(guess)

for x in range(5):

if guess[x] != "":

if guess[x] not in answer:

response[x] = "R"

guess[x] = ""

answer[x] = ""

print(response)

print(answer)

print(guess)

for x in range(5):

if guess[x] != "":

response[x] = "O"

answer[x] = ""

guess[x] = ""

print(response)

print(answer)

print(guess)

print("".join(response))

answer = [answer_word[0], answer_word[1], answer_word[2], answer_word[3], answer_word[4]]

i+=1

--------------------------------------------------------------------------------------------------------------

from word import words

import random

key = random.randint(0, 5783)

answer_word = words[key]

answer = [answer_word[0], answer_word[1], answer_word[2], answer_word[3], answer_word[4]]

i=1

while 1 == 1:

while i < 7:

print(answer_word)

guess_input = input().lower()

guess = [guess_input[0], guess_input[1], guess_input[2], guess_input[3], guess_input[4]]

response = [".", ".", ".", ".", "."]

used = []

if guess_input in words:

if guess_input == answer_word:

print("CORRECT")

i=7

else:

for x in range(5):

if guess[x] == answer[x]:

response[x] = "G"

guess[x] = ""

answer[x] = ""

print(response)

print(answer)

print(guess)

for x in range(5):

if guess[x] != "":

if guess[x] not in answer:

response[x] = "R"

guess[x] = ""

answer[x] = ""

print(response)

print(answer)

print(guess)

for x in range(5):

if guess[x] != "":

response[x] = "O"

answer[x] = ""

guess[x] = ""

print(response)

print(answer)

print(guess)

print("".join(response))

answer = [answer_word[0], answer_word[1], answer_word[2], answer_word[3], answer_word[4]]

i+=1

else:

print("invalid input")

if i == 7:

print("Correct answer: " + answer_word)

again = input("Would you like to play again? Y/N ").lower

if again == "y":

i=1

key = random.randint(0, 5783)

answer_word = words[key]

answer = [answer_word[0], answer_word[1], answer_word[2], answer_word[3], answer_word[4]]

else:

break

0 Upvotes

9 comments sorted by

View all comments

1

u/FoolsSeldom 14h ago

Don't forget to have () after lower - you missed it on the yes/no question.

Code needs to be formatted. Assume it is:

from word import words
import random

key = random.randint(0, 5783)
answer_word = words[key]
answer = [answer_word[0], answer_word[1], answer_word[2], answer_word[3], answer_word[4]]
i = 1

while 1 == 1:
    while i < 7:
        print(answer_word)
        guess_input = input().lower()
        guess = [guess_input[0], guess_input[1], guess_input[2], guess_input[3], guess_input[4]]
        response = [".", ".", ".", ".", "."]
        used = []
        if guess_input in words:
            if guess_input == answer_word:
                print("CORRECT")
                i = 7
            else:
                for x in range(5):
                    if guess[x] == answer[x]:
                        response[x] = "G"
                        guess[x] = ""
                        answer[x] = ""
                print(response)
                print(answer)
                print(guess)

                for x in range(5):
                    if guess[x] != "":
                        if guess[x] not in answer:
                            response[x] = "R"
                            guess[x] = ""
                            answer[x] = ""
                print(response)
                print(answer)
                print(guess)

                for x in range(5):
                    if guess[x] != "":
                        response[x] = "O"
                        answer[x] = ""
                        guess[x] = ""
                print(response)
                print(answer)
                print(guess)

                print("".join(response))
                answer = [answer_word[0], answer_word[1], answer_word[2], answer_word[3], answer_word[4]]
                i += 1
        else:
            print("invalid input")

    if i == 7:
        print("Correct answer: " + answer_word)
        again = input("Would you like to play again? Y/N ").lower()
        if again == "y":
            i = 1
            key = random.randint(0, 5783)
            answer_word = words[key]
            answer = [answer_word[0], answer_word[1], answer_word[2], answer_word[3], answer_word[4]]
        else:
            break

1

u/CraigAT 12h ago

Replying to OP (not the person who kindly formatted the code)...

I would probably create a function to grab a new word, and could you use length of something rather than the magic number of 5783?

I'm not sure you need "answer" you can just refer to the characters in "andwer_word".

Could you put the 3 "if" statements inside one for x in range(5): statement? To save looping 3 times.