[Tutor] Need help with code.

bob gailer bgailer at gmail.com
Sat Jun 20 02:25: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

I will throw out a few hints below. I suggest you familiarize yourself 
with "desk checking" - walking through the program by hand , writing 
down the values of variables at each step, and tracking the loops and 
conditions carefully.
>
> # 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

Note you don't offer a way to ask for a hint, you just give it.

> # Scoring system that rewards players for solving jumble without a 
> hint (not included in this code)
>
Get the rest working first.

> 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):]

OK so far. The next loop does not make sense - it uses word to iterate 
thru WORDS but does nothing with word! Position is a constant (0) so all 
it does is compare "remove dirt" with "sweeps" 4 times.

> 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

OK again. The while condition below does not make sense. word will 
always be "america" (can you see why?)

> 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()
>
The program at this point is not in any loop so it terminates.

>
> raw_input("\n\nPress the enter key to exit:")

-- 
Bob Gailer
Chapel Hill NC
919-636-4239


More information about the Tutor mailing list