I am making the game Wordle in Python and I am nearing the end. Now, I am trying to end the program whenever the user guesses the word right but whatever I am trying, nothing is working. I tried doing sys.exit()
, quit()
, SystemExit(1)
, etc, but still, nothing is working. Could you please help me solve this issue and help me end the code? Thanks.
Here is the area of my code where I need help:
winRow = [32, 32, 32, 32, 32]
if colorRow == winRow:
print("YOU WON!")
SystemExit(1)
return board, colorList, colorRow, result
numList = ["FIRST", "SECOND", "THIRD", "FOURTH", "FIFTH", "SIXTH"]
for x in range(len(numList)):
z = True
while z == True:
try:
guess = input(f"{numList[x]} GUESS > ").upper()
guess = list(guess)
checkRight(guess, word, x+1)
boardprint()
z = False
except:
boardprint()
print("Make sure your word is 5 letters")
Here is my full code:
import rich
from rich.text import Text
import random
testList = ["PRICE"]
word = random.choice(testList)
board = {
6 : ["_", "_", "_", "_", "_"],
5 : ["_", "_", "_", "_", "_"],
4 : ["_", "_", "_", "_", "_"],
3 : ["_", "_", "_", "_", "_"],
2 : ["_", "_", "_", "_", "_"],
1 : ["_", "_", "_", "_", "_"]
}
colorList = {
6 : [39, 39, 39, 39, 39],
5 : [39, 39, 39, 39, 39],
4 : [39, 39, 39, 39, 39],
3 : [39, 39, 39, 39, 39],
2 : [39, 39, 39, 39, 39],
1 : [39, 39, 39, 39, 39]
}
def boardprint():
global board, colorList
final = []
for x in range(len(board)):
spots = [f"\033[{col}m{board[x+1][idx]}\33[0m" for idx, col in enumerate(colorList[x+1])]
l = ' '.join(spots)
final.append(f"| {l} |")
ansi_rich = Text.from_ansi('\n'.join(final))
rich.print(ansi_rich)
return colorList
guess = ""
result = "playing"
def checkRight(guess, word, row):
global board, colorList
GuessCheckDouble = {i:guess.count(i) for i in guess}
WordCheckDouble = {i:word.count(i) for i in word}
result = {key: GuessCheckDouble[key] - WordCheckDouble.get(key, 0) for key in GuessCheckDouble.keys()}
updateRow = []
colorRow = []
for x in range(len(word)):
if guess[x].upper() == word[x]: # green
letter = guess[x]
updateRow.append(letter)
colorRow.append(32)
elif guess[x].upper() in word and result[guess[x]] == 0: # yellow
letter = guess[x]
updateRow.append(letter)
colorRow.append(33)
else: # grey
letter = guess[x]
updateRow.append(letter)
colorRow.append(30)
board.update({row : [updateRow[0], updateRow[1], updateRow[2], updateRow[3], updateRow[4]]})
colorList.update({row : [colorRow[0], colorRow[1], colorRow[2], colorRow[3], colorRow[4]]})
winRow = [32, 32, 32, 32, 32]
if colorRow == winRow:
print("YOU WON!")
SystemExit(1)
return board, colorList, colorRow, result
word = word.upper()
word = list(word)
print("hello! welcome to \033[32;1mWORDLE\033[0m")
print("\tTry to guess a 5-letter word in 6 guesses or less.\n\t\tIf a letter is \033[32mgreen\033[0m, then it is in the right place,\n\t\tIf a letter is \033[33myellow\033[0m, then it is in the wrong place, \n\t\tif a letter is \033[30mgrey\033[0m, then it is the wrong letter.")
print()
boardprint()
print()
numList = ["FIRST", "SECOND", "THIRD", "FOURTH", "FIFTH", "SIXTH"]
for x in range(len(numList)):
z = True
while z == True:
try:
guess = input(f"{numList[x]} GUESS > ").upper()
guess = list(guess)
checkRight(guess, word, x+1)
boardprint()
z = False
except:
boardprint()
print("Make sure your word is 5 letters")
System exiting exceptions (KeyboardInterrupt
, SystemExit
, etc) do not inherit from Exception
, so don't use a bare except
which will catch all exceptions, catch Exception
instead so that system exiting exceptions are not caught
try:
...
except Exception:
# Only handles non-system exiting exceptions
...