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

    Join Date
    Oct 2012
    Posts
    8
    Rep 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.")
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    8
    Rep 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.")
  4. #3
  5. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,997
    Rep Power
    481
    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 and Makefiles!
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    8
    Rep Power
    0
    Thank you sir, this is the second time you've responded to a "clean up my code" request, much appreciated.

IMN logo majestic logo threadwatch logo seochat tools logo