pythonif-statementinputwhile-loopalphabet

Infinite loop not checking for string size and belonging to alphabet


I've been trying to make a check for if the input (guesses) belongs to the alphabet and if it's a single character for my simple hangman game, but when I try to run the program it just ignores the entire if sentence. It's been working everywhere else and I just can't find the source of the problem. Here is my code:

def eng():
    letter_list = []
    global word
    global letter
    g = 0
    lives = 10
    while True:
        word = input("Insert The Word: ")
        if not word.isalpha():
            print("Only letters of the English alphabet are allowed")
        else:
            print(letter)
            break
    cls = lambda: print('\n' * 256)
    cls()
    ready_letters = list(set(word.lower()))
    while True:
        q = len(ready_letters)
        print(q)
        while True:
            letter = input("Your guess: ")
            if not letter.isalpha() and len(letter) != 1:
                print("You can make a guess with only one letter of the English alphabet")
            else:
                break
        print(letter_list)
        if letter in ready_letters and letter not in letter_list:
            letter_list += letter
            print("Nice")
            g += 1
            if g == q:
                print(f"The word was: {word}")
                print("GG, 🎉🎊")
                print("\n")
                return
            print(f"{g}/{q} letters guessed correctly!")
        elif letter in letter_list:
            print("You already wrote this letter, try again")
        else:
            letter_list += letter
            print("Oh noie")
            lives -= 1
            print(f"You have {lives} lives left")
            if lives == 0:
                print("GG, 웃💀")
                return

(read comment)

General tips not related to the issue would also be appreciated. Thanks for your time!


Solution

  • Simple mistake, instead of using and you should be using or. You want to print our your error message if they type a non-alpha character OR they type more than one letter.