I'm working my way through Michael Dawson's Python Programming for the Absolute Beginner book. At the end of chapter 4: "for Loops, Strings and Tuples", there is a challenge that asks so "improve Word Jumble so that each word is paired with a hint. The player should be able to see the hint if he or she is stuck. Add a scoring system that rewards players who solve a jumble without asking for a hint."
Below is the code for the Word Jumble Game:
# Word Jumble # # The copmuter picks a random word and then "jumbles" it # The player has to guess the original word import random # create a sequence of words to choose from WORDS = ("python", "jumble", "easy", "difficult", "answer", "xylophone") # hints to the WORDS HINTS = ("A programming language.", "Word _____.", "Antonym of difficult.", "Antonym of easy.", "Antonym of question.", "A musical instrument.") # pick one randomly from the sequence word = random.choice(WORDS) # create a variable to use later to see if the guess is correct correct = word # create a jumbled version of the word jumble = "" while word: position = random.randrange(len(word)) jumble += word[position] word = word[:position] + word[(position + 1):] # start the game print \ """ Welcome to Word Jumble! Unscramble the letters to make a word. (Press the enter key at the prompt to quit.) """ print "The jumble is:", jumble guess = raw_input("\nYour guess: ") guess = guess.lower() while (guess != correct) and (guess != ""): print "Sorry that's not it." guess = raw_input("Your guess: ") guess = guess.lower if guess == correct : print "That's it! You guessed right! \n" print "Thanks for playing." raw_input("\n\nPress the enter key to exit.")
I created a tuple with the hints in it. The indices of the hints are equivalent to the indices in words. Though I'm not really sure if that would work. Previous chapters covered: types, variables, branching and while loops. I'm also just skipping past this challenge and moving on to the next chapters. But its really bugging me that I did not solve this challenge.