line 45 is missing () to call lower.
pyTony
pyMod
6,314 posts since Apr 2010
Reputation Points: 879
Solved Threads: 987
Skill Endorsements: 26
I would have used a list of pairs, or maybe if there are a lot of options and looking up the hints is important, a dictionary, though that isn't the right thing here. Either of:
words_and_hints = [('python','snake or language'), ...]
# or (not really appropriate for this exercise)
words_to_hints = {'python':'sometimes eats prey or programmers for lunch',...}
Then at line 16: word,hint = random.choice(words_and_hints) When the user guesses at line 40, they can instead type a '?' (or whatever you prefer) which causes the program to display the hint and reduce the possible score for that round.
griswolf
Veteran Poster
1,176 posts since Apr 2010
Reputation Points: 344
Solved Threads: 262
Skill Endorsements: 1
pyTony
pyMod
6,314 posts since Apr 2010
Reputation Points: 879
Solved Threads: 987
Skill Endorsements: 26
Try adding lines "41.5" and "45.5" add print('You guessed "%s"'%guess) to see if there is something odd happening with the input.
By the way, to avoid the duplicate raw_input lines, you can use a loop like this, so you can call raw_input() just once at the top of the loop:
while True:
guess = raw_input("Guess a word: ")
lowguess = guess.lower()
if lowguess == correct.lower():
print("You guessed correctly!")
break
else:
print('Sorry: Your guess of "%s" is not correct.'%guess)
(after the next round of changes, if you still have problems, it might be better to post the whole thing again, so line numbers stay in sync)
griswolf
Veteran Poster
1,176 posts since Apr 2010
Reputation Points: 344
Solved Threads: 262
Skill Endorsements: 1
Post the code as it is now. Note that you should now be using a tuple, so code has to be changed, like this example:
# pick one randomly from the sequence
word_tuple = random.choice(words_and_hints)
# create a variable to use later to see if the guess is correct
word = word_tuple[0]
correct = word
woooee
Posting Maven
2,707 posts since Dec 2006
Reputation Points: 827
Solved Threads: 780
Skill Endorsements: 9
All that is left is to provide the scoring system... and maybe enjoy a little bit of tweaking. For instance here's how I would write a function to jumble the word letters:
def jumble(word):
lword = list(word)
random.shuffle(lword)
return ''.join(lword)
The other thing I'd do is add an outer loop so the game keeps going until the player is done:
keep_asking = True
total_score = 0
loop_count = 0
while keep_asking:
loop_count += 1
# do one round of guessing, adding to the score
keep_asking = raw_input("Guess again? ").lower().startswith('y')
print("Thanks for playing. Your score for %d rounds is %d"%(loop_count,total_score))
griswolf
Veteran Poster
1,176 posts since Apr 2010
Reputation Points: 344
Solved Threads: 262
Skill Endorsements: 1
Here is how I would write (have written today :) the jumble of word:
jumble = ''.join(random.sample(correct,len(correct)))
My correct is just word.lower(), and I do not call lower() for the target every time inside the loop.
I put the choosing of new word to function, which I call when points of correct answer is 0. After choosing I put the points to 10, for hint I cut that in half. I keep list of already chosen guesses and do not repeat those. Problem is also exhaustion of word list, which is not true in real game with reasonable number of guestions. Needed one extra quit condition from the game for that.
End of one game:
New word chosen!
The jumble 4: seya
Guess a word: easy
You guessed correctly! You got 10 points, your total is 35!
(Press the enter key at the prompt to quit the game.)
New word chosen!
The jumble 5: wsnaer
Guess a word: ?
Antonym of question.
The jumble 5: wsnaer
Guess a word: answer
You guessed correctly! You got 5 points, your total is 40!
(Press the enter key at the prompt to quit the game.)
New word chosen!
The jumble 6: ooylhxnep
Guess a word: xylophone
You guessed correctly! You got 10 points, your total is 50!
(Press the enter key at the prompt to quit the game.)
You answered all my guestions
Thanks for playing.
Your final result was 50.
Press the enter key to exit.
>>>
pyTony
pyMod
6,314 posts since Apr 2010
Reputation Points: 879
Solved Threads: 987
Skill Endorsements: 26
Question Answered as of 2 Years Ago by
pyTony,
griswolf
and
woooee