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 February 24th, 2004, 01:12 PM
briangw briangw is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2004
Posts: 17 briangw User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation 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

Reply With Quote
  #2  
Old February 24th, 2004, 01:26 PM
netytan's Avatar
netytan netytan is offline
Hello World :)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Mar 2003
Location: Hull, UK
Posts: 2,537 netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 18 h 17 m 47 sec
Reputation Power: 68
Send a message via ICQ to netytan Send a message via AIM to netytan Send a message via MSN to netytan Send a message via Yahoo to netytan
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


Reply With Quote
  #3  
Old February 24th, 2004, 01:38 PM
netytan's Avatar
netytan netytan is offline
Hello World :)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Mar 2003
Location: Hull, UK
Posts: 2,537 netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 18 h 17 m 47 sec
Reputation Power: 68
Send a message via ICQ to netytan Send a message via AIM to netytan Send a message via MSN to netytan Send a message via Yahoo to netytan
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.

Reply With Quote
  #4  
Old February 24th, 2004, 01:51 PM
briangw briangw is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2004
Posts: 17 briangw User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation 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?

Reply With Quote
  #5  
Old February 24th, 2004, 02:31 PM
netytan's Avatar
netytan netytan is offline
Hello World :)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Mar 2003
Location: Hull, UK
Posts: 2,537 netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 18 h 17 m 47 sec
Reputation Power: 68
Send a message via ICQ to netytan Send a message via AIM to netytan Send a message via MSN to netytan Send a message via Yahoo to netytan
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 02:33 PM.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > How to extract letters from a word

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