Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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!

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading