I am a beginner programmer with not much experience, the little I do have is with Python specifically.
With all the fuss going around about the popular word game "Wordle" I wanted to challenge myself with the task of creating my own clone version of it. I have all of the functionality I wanted working, for example, basic input filtering.
One of the main concerns I have while programming is the fear of just writing pure unreadable spaghetti code that would never be accepted by anyone who has a lot more experience than myself.
Another one of my main concerns is basic naming conventions and basic code format. Seemingly there is no absolute correct variable naming conventions (to my knowledge) so I went with lower case function names with underscores (def word_validation) and camelCase variable names.
As for the basic layout for code I know not required for Python it is still good to have a Main method but I am not sure that I call it from the bottom of my program, where I currently am.
Basically, I am asking if anyone has any suggestions on formatting for my code, minor optimizations I can make, or telling me I wrote spaghetti and how to maybe clean it up.
GAME OF WORDLE
This game chooses a random 5 letter word from
wordlib.txt
and assigns it as the "Wordle".
wordlib.txt
is a text file with a new 5 letter word on each line.The player has six chances to guess the correct word.
Each round you will receive basic information about your guess.
"g" represents a letter that is correct and is also in the correct position in the word.
EXAMPLE:
Wordle:
Slate
Guess:Clamp
Output:xggxx
"y" represents a correct letter but is not in the correct position in the word.
EXAMPLE:
Wordle:
Slate
Guess:Aspen
Output:yyxyx
"x" represents an incorrect letter
EXAMPLE:
Wordle:
Slate
Guess:Horns
Output:xxxxy
import random
ATTEMPTS = 5
attempt_number = 5
word_length = 5
is_valid = False
reset = False
guess_dict = {
"0": "last",
"1": "fifth",
"2": "forth",
"3": "third",
"4": "seccond",
"5": "first"
}
def random_word():
wordle = random.choice(open("wordlib.txt").readlines())
wordle = wordle.replace("\n","")
print("Ssssshh.. The random word is: " + wordle + " (this is for debug purposes)")
print("All guesses must be " + str(word_length) + " letter words")
return wordle
def take_user_guess():
while is_valid == False:
guess = input("What is your " + str(guess_tracker()) + " guess: ")
guess = guess.upper()
if len(guess) != word_length:
print("Sorry, your answer must be a " + str(word_length) + " letter word.")
continue
if not guess.isalpha():
print("Sorry, your answer must only contain letters.")
continue
if check_word(guess) == False:
print("Sorry, your answer must be a real word.")
continue
event_correct_guess()
return guess
def guess_tracker():
return guess_dict[str(attempt_number)]
def event_correct_guess():
global attempt_number
if attempt_number <= 0:
event_loss()
else:
attempt_number = attempt_number - 1
def check_word(guess):
with open('wordlib.txt') as f:
if guess not in f.read():
return False
def eval_guess(guess, wordle):
result = ""
if guess != wordle:
for i in range(word_length):
if guess[i] == wordle[i]:
result += "g"
elif guess[i] in wordle:
result += "y"
elif guess[i] not in wordle:
result += "x"
print(result)
return False
else:
return True
def event_win():
print("Congrats")
def event_loss():
print("Sorry, you lost.")
event_replay()
def event_replay():
global attempt_number
playAgain = input("Do you want to play again? (Yes/No)")
if playAgain.upper() == "YES":
attempt_number = ATTEMPTS
main()
else: exit()
def main():
global reset
win = False
wordle = random_word()
while win == False:
guess = take_user_guess()
win = eval_guess(guess, wordle)
event_win()
event_replay()
if __name__ == "__main__":
main()
''.join('g' if a == b else 'y' if a in wordle else 'x' for a, b in zip(guess, wordle))
\$\endgroup\$word_length
variable is unnecessary because it is explicitly stated in the rules that the word must be exactly 5 letters long. \$\endgroup\$guess_dict
ought to containfourth
andsecond
rather than misspelt versions - especially in a word game! \$\endgroup\$guess_dict
really shouldn't be adict
, it should be alist
of the six word strings, using integer string as keys for dictionary indexing is a really inefficient way, just use a flat list and use the integers themselves as indexes directly instead. Also you have many string concatenations that would be better expressed as f-strings. \$\endgroup\$