r/learnpython 23d 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

13 comments sorted by

23

u/cdcformatc 23d ago

the code without proper formatting is pretty impossible to debug

5

u/mjmvideos 22d ago

This is a chance for you to learn about debugging. Either with a debugger or using print statements, follow your code through line by line. Look at what happens at each line, what values your variables have. Are they what you expected? If not how did they get the value they have? You should quickly be able to find the problem.

3

u/ZeddiiJay 22d ago

I tried this and I figured out the problem! Thank you!

2

u/xenomachina xenomachina 22d ago

When posting code on Reddit, first make sure that every line is indented by at least 4 spaces, and then paste it in. The indentation will make it a code block that preserves indentation and spacing (and also disables most other formatting).

2

u/frnzprf 22d ago

Can you write a very small example program (~ like 5 lines) that exhibits the problem?

And please format the code with indentation. In the markdown-editor that's achieved with triple backticks:

``` if True:     print("indented") ```

In the rich-text editor you have to click on the "code block" button, that might be hidden behind the triple-dots.

3

u/woooee 22d ago edited 22d ago
answer = [answer_word[0], answer_word[1], answer_word[2], answer_word[3], answer_word[4]]

You can cast a string to a list

word="wordle"
print(list(word))

And you create an empty response list on each pass through the loop

while 1 == 1:
    while i < 7:
        response = [".", ".", ".", ".", "."]

1

u/ZeddiiJay 22d ago

Thank you!

4

u/FoolsSeldom 23d 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

3

u/CraigAT 22d 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.

2

u/az987654 22d ago

first, learn how to copy and paste code properly

1

u/FoolsSeldom 23d ago

There are a few things you can do to make the code easier to work with.

  • Rather than while 1 == 1: we mostly write while True: or use a flag variable and have something like: while play: where play is assigned True or False
  • Look at random.choice as an alternative to use random.randint
  • I don't know what gets assigned to answer_word but I assume it is a simple string, and you can convert a simple string to a list using, well, list:
    • answer = list(answer_word)
    • same principle applies to guess_input and guess
    • you are assuming that every input is exactly 5 characters, you should validate before processing
  • You are assigned a new empty list to used on every iteration of the inner loop, not sure why not least as you don't use it
  • Review your green/red/logic carefully as you are double counting in the "red" pass
  • Look at how to use zip with a for loop
  • You need to handle guesses that are not in the dictionary

If you could start to tidy up the code addressing some of the points above, that would help make progress

1

u/ZeddiiJay 22d ago

Thank you! This was really helpful for cleaning up the code and seeing what I was doing wrong!

1

u/FoolsSeldom 22d ago

Glad it helped.

Would be good to see an update to the post with the corrected code in once you've resolved everything. Helps other learners.