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 October 14th, 2012, 09:45 AM
TheLetterZero TheLetterZero is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2012
Posts: 8 TheLetterZero User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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.")

Reply With Quote
  #2  
Old October 14th, 2012, 11:56 AM
TheLetterZero TheLetterZero is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2012
Posts: 8 TheLetterZero User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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.")

Reply With Quote
  #3  
Old October 14th, 2012, 12:51 PM
b49P23TIvg's Avatar
b49P23TIvg b49P23TIvg is offline
Contributing User
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Aug 2011
Posts: 3,372 b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 3 Days 11 h 39 m 38 sec
Reputation Power: 383
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!

Reply With Quote
  #4  
Old October 14th, 2012, 01:24 PM
TheLetterZero TheLetterZero is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2012
Posts: 8 TheLetterZero User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > Need some loop help

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