HeidiHeff

Word Jumble 2.0

Nov 11th, 2012
236
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.87 KB | None | 0 0
  1. # word_jumble2.py
  2. # Word Jumble 2.0
  3. #
  4. # The computer picks a random word and then "jumbles" it
  5. # The player has to guess the original word
  6. # Improved from v1.0 with hints provided and scoring system
  7. #
  8. # Heidi Heffelfinger
  9. # November 11, 2012
  10. # Programming Python for the Absolute Beginner 3rd Ed. Chapter 4
  11. # Challenge 3
  12.  
  13. import random
  14.  
  15. # create a sequence of words to choose from; make it a constant
  16. # create a hint for each word corresponding to same tuple index
  17. WORDS = ("thinker", "curving", "report", "program", "answer", "nonsmoker")
  18. HINTS = ("The word starts with 't'; famous statue by David",          
  19.          "The word starts with 'c'; the road is not straight it is ...",
  20.          "The word starts with 'r'; the third grader is presenting his" \
  21.          " ____ to the class",
  22.          "The word starts with 'p'; once I learn Python, I can ____ a computer",
  23.          "The word starts with 'a'; question and _____",
  24.          "The word starts with 'n'; I used to use cigarettes, now I am a ______")
  25.            
  26. # pick one word randomly from the sequence
  27. word = random.choice(WORDS)
  28. # select the corresponding hint
  29. hint_index = WORDS.index(word)
  30. hint = HINTS[hint_index]
  31.  
  32. # create a variable to use later to see if the guess is correct
  33. correct = word
  34.  
  35. # initialize variables for while loops
  36. jumble = ""
  37. tries = 0
  38. hints = False
  39. score = 20
  40.  
  41. # create a jumbled version of the word
  42. # continues until new version of word with "extracted" letters is empty
  43. while word:
  44.     # generates random position in word based on length of word
  45.     position = random.randrange(len(word))
  46.     # creates new version of string jumble
  47.     jumble += word[position]
  48.     # creates new version of word minus the position letter
  49.     word = word[:position] + word[(position + 1):]
  50.    
  51. # start the game
  52. print(
  53. """
  54.            Welcome to Word Jumble!
  55.            
  56.    Unscramble the letters to make a word.
  57.    20 points for total. -2 points for each additional guess.
  58.    -10 points for use of a hint.
  59. (Press the enter key at the prompt to quit.)
  60. """
  61. )
  62. print("The jumble is:", jumble)
  63.  
  64. # get the player's guess
  65. guess = input("\nYour guess: ")
  66. tries += 1
  67. while guess != correct and guess != "":
  68.     # ask player if they would like a hint
  69.     print("\nSorry, that's not it.")
  70.     if not hints:
  71.         help_me = input("Would you like the hint? (Y/N): ")
  72.         if help_me.lower() == "y" or help_me.lower() == "yes":
  73.             print("\n\tA hint:", hint)
  74.             guess = input("\nYour guess: ")
  75.             tries += 1
  76.             hints = True
  77.             score -= 12
  78.         else:
  79.             input("\nOK, no hint. Your guess: ")
  80.             tries += 1
  81.             score -= 2
  82.     else:
  83.         guess = input("\nYour guess: ")
  84.         tries += 1
  85.         score -= 2
  86.  
  87. # congratulate player
  88. if guess == correct and tries <= 1 and hints == False and score > 0:
  89.     print("\nThat's it! You guessed it in", tries, "tries.")
  90.     print("You did not use the hint.")
  91.     print("\tYour score is:", score, "out of 20 possible.")
  92. elif guess == correct and hints == False and score > 0:
  93.     print("\nThat's it! You guessed it in", tries, "tries.")
  94.     print("\nYou lost 2 points for each additional attempt.")
  95.     print("You chose not to use the hint.")
  96.     print("tYour score is:", score, "out of 20 points possible.")
  97. elif guess == correct and hints == True and score > 0:  
  98.     print("\nThat's it! You guessed it in", tries, "tries.")
  99.     print("\nYou lost 2 points for each additional attempt.")
  100.     print("You chose to use the hint for an additional 10 point loss.")
  101.     print("\tYour score is:", score, "out of 20 points possible.")
  102. else:
  103.     print("\nGAME OVER.")
  104.     print("Either you failed to guess the word, you left it blank or" \
  105.           " your score was less than zero.")
  106.    
  107. # end the game
  108. print("\nThanks for playing.")
  109.  
  110. input("\n\nPress the enter key to exit.")
Advertisement
Add Comment
Please, Sign In to add comment