#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    31
    Rep Power
    2

    Getting an IF statement to recognize very specific characters


    OK so I am trying to code a challenge game that gives the user five guesses at letters and prints them out. I am trying to get the program to recognize if the letters aren't in the range of a-z and if the inputted characters are more then one.

    Here is the first part of the program:

    Code:
    import random
    import re
    
    WORDS = ("python",
             "jumble",
             "easy",
             "difficult",
             "answer",
             "xylophone")
    
    word = random.choice(WORDS)
    
    word_length = len(word)
    
    print("""
            Welcome to 'Random Word Guess'!
                Where randomly guessing
                words is the funnest!!!!
    
            Instructions:
            I will choose a random word from
            my memory banks.  I will then give
            you five guesses at letters in the
            word.  Then you have to guess the word
            after your letter guesses run out.
            You have five guesses!  Let's start!
    """)
    
    print("The length of the random word is",word_length,"characters.")
    
    print("\nOK, begin your letter guesses!")
    
    guessed_letters = ""
    guess_tries = 0
    
    while True:
        letter_guess = input("\nYour letter guess: ")
        if not re.match("^[a-z]*$",letter_guess):
            print("\nPlease use only the letters 'a' through 'z'!")
        if len(letter_guess) > 1:
            print("\nOnly one input character allowed!")
            continue
        if letter_guess not in word:
            print("\nNo")
            guess_tries += 1
        if letter_guess in word:
            print("\nYes")
            guessed_letters = guessed_letters + letter_guess
            guess_tries += 1
        elif guess_tries == 5:
            print("\nHere are the letters you've guessed:",guessed_letters)
            print("\nThat's the end of your guesses, now lets try to guess the word.")
            break
    This particular if statement keeps breaking my while loop and messing with my guess_tries count:

    Code:
    if letter_guess not in word:
            print("\nNo")
            guess_tries += 1
    Basically what is happening I think is when a character besides a letter or when the character is greater then 1 character it still prints No and still adds the guess_tries but resets the loop and messes with the conditions for guess_tries by making it greater then 5.

    How can I get this if statement to not print and not add to guess_tries if the letter_guess is a number or a character besides a letter or if the input length is greater then 1? I have tried numerous things but I can't get the logic. The code above is the original if statement.

    Thanks for any help!!!
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    114
    Rep Power
    3
    Use the string method isalpha() to check if a character is a letter, and the built-in len() function to examine its length.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    31
    Rep Power
    2
    Thank you for your input Nyktos. I can't seem to get the combination to work. Do I have to import something?
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    31
    Rep Power
    2
    Well I changed it to this:

    Code:
    if letter_guess.isalpha() and letter_guess not in word:
            print("\nNo")
            guess_tries += 1
    And it works just fine. Thank Nyktos.

IMN logo majestic logo threadwatch logo seochat tools logo