How to skip one loop in a for loop?

Question:

I’m trying to make a really simple code that works sort of like wordle. It gets a random 5 letter word and asks the user to guess the word. The code will then output if a letter was in the word and if the letter was in the same position as the randWord.

quick flowchart of what is supposed to be happening

The problem I’m having at the moment is that when there is two of the same letters the code will give the user more of that letter then what is in the word. This is because:

enter image description here

The way the code works it runs through the word with every single letter once. So the diagram above happens twice and ends up adding 4 O’s to the list. This happens every single time there is a word with two of the same letters. I want to know if there is a way to skip over the word once 1 letter has matched. I’ve made a really terrible solution that I’m sure won’t work for words that have three of the same letter:

    if len(userInput) < len(letter):
        deleted = []
        for i in range(len(letter)):
            l = i + 1
            if letter[i] == letter[l]:
                deleted.append(letter[i])
                del letter[i]
        if len(userInput) > len(letter):
            for i in range(len(deleted)):
                l = i + 1
                if deleted[i] == deleted[l]:
                    del letter[i]
            letter.append(deleted)

It basically deletes half of the same numbers to fix the problem temporarily.

And here is the full code:

from random_word import Wordnik
wordnik_service = Wordnik()

truth = True

#grabs a random word
randWord = wordnik_service.get_random_word(minLength=5, maxLength=5)

#starts the while loop until the word has been solved
while truth == True:
    #creates an input in the terminal for the user to submit there word
    userInput = input("Guess a 5-letter word: ")
    #defining variables and lists
    letter = []
    trueLett = []
    same = False
    #create for loops to cycle through the user inputed word and the random word to find letters that match
    for i in range(len(userInput)):
        for l in range(len(randWord)):
            #checks if the letters are the same value
            if randWord[l] == userInput[i]:
                #checks if the letter are both in the same position
                if l == i:
                    same = True
                    trueLett.append(randWord[l])
                #checks if the letter isn't in the same position
                elif l != i:
                    letter.append(randWord[l])
            l+=1   
        i+=1
        
        #we don't talk about this italian dish that was cooked by a toddler
        if len(userInput) < len(letter):
            deleted = []
            for i in range(len(letter)):
                l = i + 1
                if letter[i] == letter[l]:
                    deleted.append(letter[i])
                    del letter[i]
            if len(userInput) > len(letter):
                for i in range(len(deleted)):
                    l = i + 1
                    if deleted[i] == deleted[l]:
                        del letter[i]
                letter.append(deleted)
              
    if userInput == randWord:
        print("Correct")
        truth = False
    elif userInput == 'Lose':
        print(randWord)
    elif len(userInput) > 5 or len(userInput) < 5:
        print('The word is 5-letters. Please try again')
    else:
        print("Incorrect. Try again")
        if same == True:   
            print("These letter/s: " + str(trueLett) + " are in the correct place")
        if len(letter) != 0:
            print("These letter/s: " + str(letter) + " are in the word")

I hope that is enough information, thanks in advance for helping.

Asked By: Noah Robb

||

Answers:

To answer your question:

arr = [1, 2, 3, 4]

for n in arr:
    if n == 3:  # skip condition
        continue
    print(n)

prints:

1
2
4

Though to accomplish what I think you actually want to do, might I suggest:

rand_word, guess_word = 'igloo', 'xolgi'

letters_in_word = {guess_ltr: guess_ltr in rand_word for guess_ltr in guess_word}
# => {'x': False, 'l': True, 'g': True, 'i': True}

letters_correct_pos = {
    guess_ltr: guess_ltr == ltr for guess_ltr, ltr in zip(guess_word, rand_word)
}
# => {'x': False, 'l': True, 'g': False, 'i': False}

And in case you’re not familiar with list comprehensions, the following is equivalent:

letters_in_word = {}
for guess_ltr in guess_word:
    letters_in_word[guess_ltr] = guess_ltr in rand_word

letters_correct_pos = {}
for guess_ltr, ltr in zip(guess_word, rand_word):
    letters_correct_pos[guess_ltr] = guess_ltr == ltr
Answered By: Kache
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.