Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.

Python Programming Question...How do you code...?

Okay, guys. Taking a creative computing (programming class). We're using Python as our language. I'm just a novice (noob), so I don't know anything about this. We're supposed to write a code for this word game, and I started, but I don't know what I did wrong. Below are the instructions and my code. Can you tell me what I need to do next?

Thanks!

Activity 2: Word games

Your program should pick a random word from a list you pre-define and allow the player to

guess it. The computer should tell the player how many letters are in the word. Then the player

should get 5 chances to guess a letter in the word. The computer responds either with no or with

the number of times the letter appears (you should look up the count method). After 5 suggested

letters, the user must attempt to guess the word.

Here is an example run, but feel free to get creative with the text and make any extensions you

want (user input underlined):

Welcome to an amazing word game! I am thinking of a word that

you need to guess. You can ask me whether 5 letters are in the

word.

Your letter guess: b

No, not in my word!

Your letter guess: e

Yes, 3 in my word.

Your letter guess: q

No, not in my word!

Your letter guess: h

Yes, 1 in my word.

Your letter guess: l

Yes, 1 in my word.

Your word guess: elePhant

You’re right, my word was Elephant!!

from random import*

def word_games():

word_list=["happy","festival","sticky","tunnel","keyboard","paper"]

shuffle(word_list)

secret_word=word_list[0]

print "Welcome to my word game. I'm thinking of a word. You have five chances to ask if a letter is in it or not, and at the end, you will be asked to guess my word. Good luck!"

print "Hint, my word has ", len(secret_word), "letters in it."

for i in range(5):

guess=raw_input("Your letter guess: ")

for letter in secret_word:

count=0

if (guess==letter):

count+=1

if (count>0):

print ("Yes, there are "+(count)+" in my word.")

else:

print ("No, there are none in my word.")

final_guess=raw_input("Okay, chances up. Your word guess: ")

if (final_guess==secret_word):

print "Yes, you are correct. My word is ",secret_word,".You win!"

else:

print "Noooooo, you are wrong. My word is ",secret_word,".You lose."

1 Answer

Relevance
  • om
    Lv 6
    1 decade ago
    Favorite Answer

    It'd be easier to help if you told us exactly how your program was misbehaving. That is: what you did, what expected to happen, and what actually happened.

    As it happens, your program is pretty darned good for a self-described novice. It's very close to working correctly. As far as I can tell there are only two small things wrong. (I'm not counting the missing space between 'import' and '*', I assume that's just a typo or a pasting problem.)

    The first is that 'count=0' is in the wrong place. It should be on the line before the 'for' statement. Right now the program zeroes 'count' every time it runs the loop, so the program forgets how many times it has seen the guess letter. It will only remember if the very last letter of the word matches the guess.

    The second is that in this line:

      print ("Yes, there are "+(count)+" in my word.")

    Python will object to your trying to add a number to a string. The easiest way to fix that is to explicitly build a string from the number and then add that string to the other strings. Like this:

      print ("Yes, there are " + str (count) + " in my word.")

    Since you already have parentheses around '(count)' that might just be another typo.

    The only other issue, which I'll mention only because it has confused a few other people on Y!A in the past, is that as it stands your program defines a function but it doesn't ever try to actually run the function. If you don't already have a statement outside the function definition that does:

      word_games()

    to run the function, then you need to add that.

    After you've got the program working, there is a small improvement you could make. (This is entirely optional, it does not change the overall behaviour of the program.) Instead of using shuffle() to scramble the entire word list and then picking the first word from the shuffled list, it would be more efficient to use choice() to just pick a random word from the list. It's the equivalent of just picking out a random card from a deck, versus shuffling the deck and then picking the top card. The results are equivalent but the amount of work performed is different. It's not a big deal in a small program like this, but in a program that does a lot of this kind of thing with large lists the difference can be important.

    Source(s): The difference between shuffle() and choice(): http://docs.python.org/library/random.html
Still have questions? Get your answers by asking now.