The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> Python Programming
|
Need some loop help
Discuss Need some loop help in the Python Programming forum on Dev Shed. Need some loop help 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:
|
|
|

October 14th, 2012, 09:45 AM
|
|
Registered User
|
|
Join Date: Oct 2012
Posts: 8
Time spent in forums: 4 h 58 m 58 sec
Reputation Power: 0
|
|
|
Need some loop help
Ok, I have to write a program where the computer picks a word at random, computer tells how many characters are in the word and the player gets five chances to guess a letter in the word, computer responds yes or no if the guessed letter is in the word. After five letter guesses, the player has to guess word.
I've spent probably way to much time on this but this is as close as I can get the program to do what I want it to do. The trouble I am having is with the letter guessing part.
I am using the for loop, the only letter that shows up as a correct guess is the first letter in the chosen word. I have gotten it to correctly identify other letters in the word by playing with the placement of the for loop and the indentations in my numerous attempts to troubleshoot but then the program begins to do other things I do not intend it to do.
After re-reading the chapter in the book I got this challenge from, the real use of the for loop became a little clearer to me and Im 99% sure this is not what I am supposed to be using here, either that or I am just implementing it wrong? What am I missing, I feel like it's something really fundamental but am just having some trouble figuring out what it is.
Help is much appreciated.
Here's what I've got so far:
Code:
#Word Guess Game
#Computer picks a random word, player has to guess that word
print("Welcome to the Word Guess Game!")
print("I'll pick a word at random, and tell you how many letters")
print("are in the word. You'll get five chances to ask what letters")
print("are in the word, after that you must guess the word.")
print("\nGood Luck!")
import random
WORDS = ("python", "jumble", "easy", "difficult", "answer", "xylophone")
word = random.choice(WORDS)
length = len(word)
guesses = 0
correct = word
print("Okay, I've picked a word...")
print("It is:", length, "characters long.")
print("\nNow you have 5 guesses as to what letters are in the word.")
guess = input("\nGuess a letter: ")
guesses += 1
for letter in word:
while guesses <= 4:
if guess != letter:
print("\nNo", guess,"is not a letter in the word.")
print("You have taken", guesses,"guess(es) so far.")
guess = input("Guess another letter.")
guesses += 1
elif guess == letter:
print("\nYes!", guess,"is a letter in the world!")
print("You have taken", guesses,"guess(es) so far.")
guess = input("Guess another letter.")
guesses += 1
if guess != letter and guesses == 5:
print("\nNo", guess,"is not a letter in the word.")
print("You have taken all", guesses,"guesses, time to guess the word.")
wordguess = input("\nGo ahead, take a guess: ")
elif guess == letter and guesses == 5:
print("\nYes!", guess,"is a letter in the world!")
print("You have taken all", guesses,"guesses, time to guess the word.")
wordguess = input("\nGo ahead, take a guess: ")
while wordguess != correct:
print("\nThat is not the word.")
wordguess = input("\nGuess again: ")
if wordguess == correct:
print("Good work!", wordguess,"was the word!")
input("\n\nPress the enter key to exit.")
|

October 14th, 2012, 11:56 AM
|
|
Registered User
|
|
Join Date: Oct 2012
Posts: 8
Time spent in forums: 4 h 58 m 58 sec
Reputation Power: 0
|
|
Well I've figured it out. I needed to use the in operator correctly. The for loop was totally unnecessary.
Now I can move on to the next chapter.
If anyone has any suggestions to clean up and simplify my code, they'd be very welcome!
Here's my new code:
Code:
#Word Guess Game
#Computer picks a random word, player has to guess that word
print("Welcome to the Word Guess Game!")
print("I'll pick a word at random, and tell you how many letters")
print("are in the word. You'll get five chances to ask what letters")
print("are in the word, after that you must guess the word.")
print("\nGood Luck!")
import random
WORDS = ("python", "jumble", "easy", "difficult", "answer", "xylophone")
word = random.choice(WORDS)
length = len(word)
guesses = 0
correct = word
print("Okay, I've picked a word...")
print("It is:", length, "characters long.")
print("\nNow you have 5 guesses as to what letters are in the word.")
guess = input("\nGuess a letter: ")
guesses += 1
while guesses <= 4:
if guess not in word:
print("\nNo", guess,"is not a letter in the word.")
print("You have taken", guesses,"guess(es) so far.")
guess = input("Guess another letter.")
guesses += 1
elif guess in word:
print("\nYes!", guess,"is a letter in the world!")
print("You have taken", guesses,"guess(es) so far.")
guess = input("Guess another letter.")
guesses += 1
if guess not in word and guesses == 5:
print("\nNo", guess,"is not a letter in the word.")
print("You have taken all", guesses,"guesses, time to guess the word.")
wordguess = input("\nGo ahead, take a guess: ")
elif guess in word and guesses == 5:
print("\nYes!", guess,"is a letter in the world!")
print("You have taken all", guesses,"guesses, time to guess the word.")
wordguess = input("\nGo ahead, take a guess: ")
while wordguess != correct:
print("\nThat is not the word.")
wordguess = input("\nGuess again: ")
if wordguess == correct:
print("Good work!", wordguess,"was the word!")
input("\n\nPress the enter key to exit.")
|

October 14th, 2012, 12:51 PM
|
 |
Contributing User
|
|
|
|
Without using functions, we can make some direct simplifications. See comments.
Code:
#This is a python3 program
#Word Guess Game
#Computer picks a random word, player has to guess that word
print("Welcome to the Word Guess Game!")
print("I'll pick a word at random, and tell you how many letters")
print("are in the word. You'll get five chances to ask what letters")
print("are in the word, after that you must guess the word.")
print("\nGood Luck!")
import random
WORDS = ("python", "jumble", "easy", "difficult", "answer", "xylophone")
word = random.choice(WORDS)
length = len(word)
guesses = 0
################correct and word are always the same. I removed correct.
print("Okay, I've picked a word...")
print("It is:", length, "characters long.")
print("\nNow you have 5 guesses as to what letters are in the word.")
guess = input("\nGuess a letter: ")
guesses += 1
while guesses < 5:
# reordered code to eliminate `not'
if guess in word:
print("\nYes!", guess,"is a letter in the world!")
#elif guess in word: # You already decided this binary choice, don't test again!
else:
print("\nNo", guess,"is not a letter in the word.")
# the next three lines are duplicate code and they always take place, don't repeat them!
print("You have taken", guesses,"guess(es) so far.")
guess = input("Guess another letter.")
guesses += 1
# how can the program arrive here with guesses having a value different from 5?
# Answer, it cannot.
if guess in word: ######################removed--->and guesses == 5:
print("\nYes!", guess,"is a letter in the world!")
else:
print("\nNo", guess,"is not a letter in the word.")
# once again duplicate code moved to higher block level
print("You have taken all", guesses,"guesses, time to guess the word.")
wordguess = input("\nGo ahead, take a guess: ")
while wordguess != word:
print("\nThat is not the word.")
wordguess = input("\nGuess again: ")
################removed---> if wordguess == correct: ################this test must be true
print("Good work!", wordguess,"was the word!")
input("\n\nPress the enter key to exit.")
__________________
[code] Code tags[/code] are essential for python code!
|

October 14th, 2012, 01:24 PM
|
|
Registered User
|
|
Join Date: Oct 2012
Posts: 8
Time spent in forums: 4 h 58 m 58 sec
Reputation Power: 0
|
|
|
Thank you sir, this is the second time you've responded to a "clean up my code" request, much appreciated.
|
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
|
|
|
|
|