From bbc47c4fafed7cbcfb3a6c730f369b2e20499a90 Mon Sep 17 00:00:00 2001 From: Conor Walker <39270500+conor-walker@users.noreply.github.com> Date: Tue, 25 Jan 2022 21:23:48 +0000 Subject: [PATCH] Create main.py --- main.py | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 main.py diff --git a/main.py b/main.py new file mode 100644 index 0000000..5ba4d61 --- /dev/null +++ b/main.py @@ -0,0 +1,49 @@ +def getGuess(): + validGuess = False + guess = "" + while not validGuess: + guess = str(input("Please guess:")).upper() + if len(guess) > 5 or len(guess) < 5: + print("Invalid guess - please ensure you're guessing a word of 5 letters") + else: + validGuess = True + return guess + + +def checkRight(wordToGuess, guessSoFar): + if str(wordToGuess).upper() == str(guessSoFar).upper(): + return True + else: + return False + + +emptyWordGuess = ["_"]*5 +wrongLetters = set() +rightLettersWrongPlace = set() +wordToGuess = ["P", "L", "A", "N", "S"] + +userGuess = getGuess() +counter = 0 + +while counter < 5: + for i in range(len(userGuess)): + if wordToGuess[i] == userGuess[i]: + emptyWordGuess[i] = userGuess[i] + elif userGuess[i] in wordToGuess: + if userGuess[i] in emptyWordGuess and userGuess[i] not in wordToGuess[i:]: + break + else: + rightLettersWrongPlace.add(userGuess[i]) + else: + wrongLetters.add(userGuess[i]) + if checkRight(wordToGuess, emptyWordGuess): + print("Congratulations! The word was", ''.join(wordToGuess).capitalize()) + exit(420) + print("Incorrect letters: " + str(wrongLetters)) + print("Correct letters in the wrong place: " + str(rightLettersWrongPlace)) + print("Result: " + " ".join(emptyWordGuess)) + counter += 1 + userGuess = getGuess() + +print("Ran out of guesses! The word was: ", "".join(wordToGuess)) +exit(69)