Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old December 28th, 2012, 08:33 PM
jomiscli jomiscli is offline
Registered User
Click here for more information
 
Join Date: Dec 2012
Posts: 29 jomiscli User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 11 h 39 sec
Reputation Power: 0
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!!!

Reply With Quote
  #2  
Old December 28th, 2012, 09:10 PM
Nyktos Nyktos is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2012
Posts: 88 Nyktos User rank is Corporal (100 - 500 Reputation Level)Nyktos User rank is Corporal (100 - 500 Reputation Level)Nyktos User rank is Corporal (100 - 500 Reputation Level)Nyktos User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 1 Day 5 h 29 m 39 sec
Reputation Power: 2
Use the string method isalpha() to check if a character is a letter, and the built-in len() function to examine its length.

Reply With Quote
  #3  
Old December 28th, 2012, 09:23 PM
jomiscli jomiscli is offline
Registered User
Click here for more information
 
Join Date: Dec 2012
Posts: 29 jomiscli User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 11 h 39 sec
Reputation Power: 0
Thank you for your input Nyktos. I can't seem to get the combination to work. Do I have to import something?

Reply With Quote
  #4  
Old December 29th, 2012, 01:49 AM
jomiscli jomiscli is offline
Registered User
Click here for more information
 
Join Date: Dec 2012
Posts: 29 jomiscli User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 11 h 39 sec
Reputation Power: 0
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.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > Getting an IF statement to recognize very specific characters

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap