Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # word_jumble2.py
- # Word Jumble 2.0
- #
- # The computer picks a random word and then "jumbles" it
- # The player has to guess the original word
- # Improved from v1.0 with hints provided and scoring system
- #
- # Heidi Heffelfinger
- # November 11, 2012
- # Programming Python for the Absolute Beginner 3rd Ed. Chapter 4
- # Challenge 3
- import random
- # create a sequence of words to choose from; make it a constant
- # create a hint for each word corresponding to same tuple index
- WORDS = ("thinker", "curving", "report", "program", "answer", "nonsmoker")
- HINTS = ("The word starts with 't'; famous statue by David",
- "The word starts with 'c'; the road is not straight it is ...",
- "The word starts with 'r'; the third grader is presenting his" \
- " ____ to the class",
- "The word starts with 'p'; once I learn Python, I can ____ a computer",
- "The word starts with 'a'; question and _____",
- "The word starts with 'n'; I used to use cigarettes, now I am a ______")
- # pick one word randomly from the sequence
- word = random.choice(WORDS)
- # select the corresponding hint
- hint_index = WORDS.index(word)
- hint = HINTS[hint_index]
- # create a variable to use later to see if the guess is correct
- correct = word
- # initialize variables for while loops
- jumble = ""
- tries = 0
- hints = False
- score = 20
- # create a jumbled version of the word
- # continues until new version of word with "extracted" letters is empty
- while word:
- # generates random position in word based on length of word
- position = random.randrange(len(word))
- # creates new version of string jumble
- jumble += word[position]
- # creates new version of word minus the position letter
- word = word[:position] + word[(position + 1):]
- # start the game
- print(
- """
- Welcome to Word Jumble!
- Unscramble the letters to make a word.
- 20 points for total. -2 points for each additional guess.
- -10 points for use of a hint.
- (Press the enter key at the prompt to quit.)
- """
- )
- print("The jumble is:", jumble)
- # get the player's guess
- guess = input("\nYour guess: ")
- tries += 1
- while guess != correct and guess != "":
- # ask player if they would like a hint
- print("\nSorry, that's not it.")
- if not hints:
- help_me = input("Would you like the hint? (Y/N): ")
- if help_me.lower() == "y" or help_me.lower() == "yes":
- print("\n\tA hint:", hint)
- guess = input("\nYour guess: ")
- tries += 1
- hints = True
- score -= 12
- else:
- input("\nOK, no hint. Your guess: ")
- tries += 1
- score -= 2
- else:
- guess = input("\nYour guess: ")
- tries += 1
- score -= 2
- # congratulate player
- if guess == correct and tries <= 1 and hints == False and score > 0:
- print("\nThat's it! You guessed it in", tries, "tries.")
- print("You did not use the hint.")
- print("\tYour score is:", score, "out of 20 possible.")
- elif guess == correct and hints == False and score > 0:
- print("\nThat's it! You guessed it in", tries, "tries.")
- print("\nYou lost 2 points for each additional attempt.")
- print("You chose not to use the hint.")
- print("tYour score is:", score, "out of 20 points possible.")
- elif guess == correct and hints == True and score > 0:
- print("\nThat's it! You guessed it in", tries, "tries.")
- print("\nYou lost 2 points for each additional attempt.")
- print("You chose to use the hint for an additional 10 point loss.")
- print("\tYour score is:", score, "out of 20 points possible.")
- else:
- print("\nGAME OVER.")
- print("Either you failed to guess the word, you left it blank or" \
- " your score was less than zero.")
- # end the game
- print("\nThanks for playing.")
- input("\n\nPress the enter key to exit.")
Advertisement
Add Comment
Please, Sign In to add comment