I am working on creating a simple “Lingo” like game in Python where a 5 letter word is generated and the first letter of that word is shown. Like the game show, you guess a word and if any letters are correct, those letters are revealed. You get 5 tries at guessing the correct word. In my program, if the correct word is guessed before the “5th” attempt, the loop is supposed to exit, and “You win!” is supposed to be printed on the screen.
However, the if-statement that is supposed to execute that code doesn’t work when the correct word is guessed. I have debugged my code and checked to make sure that the “answer” string can be seen inside the loop along with the “randomword” string and both strings are visible where the if-statement takes place. I have printed both of these values out inside of my loop and they match. So, the if(answer == randomword) statement should be executed when a correct guess is given, but for some reason, it is not. Any help would be appreciated.
import random file = "words.txt" #File with 5 letter words f = open(file, 'r') lines = f.readlines() randomword = random.choice(lines) #generate random 5 letter word from file word = (randomword(0), ' ', ' ', ' ', ' ')#part of word that is being shown to user print(randomword) print(randomword(0)) for i in range(1,6): #take 5 guesses at what random word is guess = input("Guess the word: ") for j in range(1,5): #check to see if other 4 letters of random word match the letters in the guess word if(guess(j) == randomword(j) and word(j) == ' '): #replace empty character with letter of random word if correctly guessed word(j) = randomword(j) answer = word(0) + word(1) + word(2) + word(3) + word(4) #convert word array to string to be shown to user print(answer) if(answer == randomword): print("You win!") exit if(answer != randomword): print("You loose!")