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

    Join Date
    Feb 2004
    Posts
    17
    Rep Power
    0

    How to extract letters from a word


    I have another question. This portion is not pulling a letter out of a random string of words that I have placed in the program for the program to pull a random word up and have a user guess 5 letters.

    I guess what I'm missing is the command to have the program check for a letter and to inform the user if it's in the word or not. I have not defined "letter," but letter seems to be more of a part of the Python code instead of a variable.

    Code:
    tries = 0
    word = random.choice(WORDS)
    for letter in word:
        guess = raw_input("Please guess a letter of that word. You can guess up to 5 times: ")
        print
        tries += 1
        if letter in word:
            print "Yes."
            print
        else:
            print "No."
            print
        if tries == 5:
            break
  2. #2
  3. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    Just a guess but this pogram just say 'Yes' right. The reason for this is you're checking if letter is in word, and letter is always in the word . What you want to do is check if guess is in word i.e.

    Code:
    ...
    ...
    if guess in word:
        print 'Yes.'
        print
    else:
        print 'No.'
        print
    ...
    ...
    Let me know how this goes,

    Mark.
    programming language development: www.netytan.com Hula

  4. #3
  5. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    A revided version of your using a while loop. This code actually lets you get up to 5 wrong answers, kinda like a basic hangman game.

    Code:
    #!/usr/bin/env pythpn
    
    from random import choice
    
    loop = 0
    word = choice(['netytan', 'python'])
    
    while loop <= 5:
    
        guess = raw_input('Please enter a letter: ')
    
        if guess in word:
            print 'Yes\n'
        else:
            print 'No\n'
    
            loop = loop + 1
    Mark.
    programming language development: www.netytan.com Hula

  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2004
    Posts
    17
    Rep Power
    0
    Ok, now that fixed it, however, you removed my "tries == 5 and break statement, so I'm getting caught in a loop and not able to break out of it, so the player can guess the word. I didn't add the rest of the code, since the rest of the code is working, it's just that the loop was not.

    Code:
    import random
    
    guess = 0
    tries = 0
    word = random.choice(WORDS)
    correct = word
    while tries <= 5:
    
        guess = raw_input('Please enter a letter: ')
        if guess in word:
            print 'Yes\n'
    
        else:
            print 'No\n'
            tries = tries + 1
        if tries == 5:
            break
    print "You've exhausted your guesses."
    
    guess2 = raw_input("\nPlease guess the word: ")
    guess2 = guess2.lower()
    while (guess2 != correct) and (guess2 != ""):
        print "Sorry, that is not the word."
        guess2 = raw_input("Please take another guess: ") 
        guess2 = guess2.lower()
    if guess2 == correct:
            print "Congratulations!!! You guessed the correct word!\n"
    Now, the bold part was working before, why won't it now?
  8. #5
  9. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    What the gode i gave you is saying is you can guess wrong five times before the loop ends, hence the "kinda to hangman".

    If you want to make it equivelent to you're other code just unindent the loop = loop + 1 line i.e.

    Code:
    #!/usr/bin/env pythpn
    
    from random import choice
    
    loop = 0
    word = choice(['never', 'ever'])
    
    while loop <= 5:
    
        guess = raw_input('Please enter a letter: ')
    
        if guess in word:
            print 'Yes\n'
        else:
            print 'No\n'
    
        loop = loop + 1
    This way, the loop will run though a MAX of five times and no more. So as i see it all you need to do is add one to 'tries' each time the loop runs though.

    Mark.
    Last edited by netytan; February 24th, 2004 at 03:33 PM.
    programming language development: www.netytan.com Hula


IMN logo majestic logo threadwatch logo seochat tools logo