The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> Python Programming
|
How to extract letters from a word
Discuss How to extract letters from a word in the Python Programming forum on Dev Shed. How to extract letters from a word Python Programming forum discussing coding techniques, tips and tricks, and Zope related information. Python was designed from the ground up to be a completely object-oriented programming language.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

February 24th, 2004, 01:12 PM
|
|
Registered User
|
|
Join Date: Feb 2004
Posts: 17
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
|

February 24th, 2004, 01:26 PM
|
 |
Hello World :)
|
|
Join Date: Mar 2003
Location: Hull, UK
|
|
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
|

February 24th, 2004, 01:38 PM
|
 |
Hello World :)
|
|
Join Date: Mar 2003
Location: Hull, UK
|
|
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.
|

February 24th, 2004, 01:51 PM
|
|
Registered User
|
|
Join Date: Feb 2004
Posts: 17
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?
|

February 24th, 2004, 02:31 PM
|
 |
Hello World :)
|
|
Join Date: Mar 2003
Location: Hull, UK
|
|
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.
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|