[Tutor] Need help with code.

Dave Angel davea at ieee.org
Sat Jun 20 02:27:42 CEST 2009


Raj Medhekar wrote:

> Hi, I need help with the code below. I am trying to pair the list in the hint with the list of words. However, the list is not matching up correctly. Also when the correct guess is entered the program just continues without acknowledging the right answer. Please could y'all test this out and let me know where I am going wrong.  I also want to add a scoring system where the computer rewards the person that guesses without getting a hint. Any suggestions as to how I might go about incorporating that into my code. Thanks for the help!
>
> Peace,
> Raj
>
> # Word Jumble V2
> # Computer jumbles a randomly selected word then asks the player to guess it
> # If player cant guess the word he can ask for a hint
> # Scoring system that rewards players for solving jumble without a hint (not included in this code)
>
> import random
>
> WORDS = ("sweep","difficult", "python", "america")
> HINTS = ("remove dirt","not easy","computer language and a snake",
>          "land of the free")
>
> word = random.choice(WORDS)
> correct = word
> jumble = ""
>
> while word:
>     position = random.randrange(len(word))
>     jumble += word[position]
>     word = word[:position] + word[(position+1):]
> for word in WORDS:
>     HINTS[position] == WORDS[position]
>
> print\
>     """
>         Welcome to Word Jumble
> Unscramble the letters to make a word
> Remember Hints lower your score
> (Press the enter key at the prompt to exit)
>     """
> print "The jumble is: ",jumble
> guess = raw_input("Guess the word: ")
> guess = guess.lower()
> tries = 1
> while guess != word and word !="" and tries <5:
>     print "Sorry! That's not it!"
>     guess = raw_input("Guess the word: ")
>     guess = guess.lower()
>     tries +=1
> if guess == correct:
>     print "You're right, the word is", word
> else:
>     print "Maybe you need a hint:"
>     print HINTS[position]
>     guess = raw_input("Guess the word: ")
>     guess = guess.lower()
>
>
> raw_input("\n\nPress the enter key to exit:")
>
>
>   
To start with, what did you possibly hope to do with:

for word in WORDS:
    HINTS[position] == WORDS[position]

The body of that loop does nothing at all.

What you really wanted was:
position = WORDS.index(correct)

Next, examine the phrase:

while guess != word and word !="" and tries <5:
    print "Sorry! That's not it!"


Why would you be comparing 'guess' against the highest word in the 
list?  Shouldn't you be comparing it to 'correct' ?






More information about the Tutor mailing list