HeidiHeff

Guess the Word

Nov 11th, 2012
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.05 KB | None | 0 0
  1. # word_guess.py
  2. # Guess The Word
  3. #
  4. # The computer picks a random word and the player has to guess the word.
  5. # The computer tells the player how many letters are in the word.
  6. # The player has 5 chances to ask if a letter is in the word.f
  7. # The player must then guess the word.
  8. #
  9. # Heidi Heffelfinger
  10. # November 11, 2012
  11. # Programming Python for the Absolute Beginner 3rd Ed. Chapter 4
  12. # Challenge 4
  13.  
  14. import random
  15.  
  16. # create a sequence of words to choose from; make it a constant
  17. WORDS = ("adult", "knife", "easy", "magnet", "answer", "penal)
  18.  
  19. # pick one word randomly from the sequence
  20. word = random.choice(WORDS)
  21.  
  22. # create a variable to use later to see if the guess is correct
  23. correct = word
  24.  
  25. # find length of the word
  26. letters = len(word)
  27.    
  28. # start the game
  29. print(
  30. """
  31.             Welcome to Guess the Word!
  32.            
  33.     Ask which letters are in a randomly chosen word
  34.     You have 5 chances to ask which letters are in the word.
  35.     You then guess the word.
  36. (Press the enter key at the prompt to quit.)
  37. """
  38. )
  39. print("I'm thinking of a word.")
  40. print("There are,", letters, "letters in the word.\n")
  41.  
  42. #initialize variables for while loop
  43. counter = 0
  44. letter_guess_length = 1
  45.  
  46. # player asks about which letters are in the word
  47. while counter < 5:
  48.    if letter_guess_length == 1:
  49.        letter_guess = input("Ask about a letter in the word: ")
  50.        letter_guess_length = len(letter_guess)
  51.        letter_guess = letter_guess.lower()
  52.        counter += 1
  53.        if letter_guess in word:
  54.            print("\n\tYes,", letter_guess, "is in the word.")
  55.        else:
  56.            print("\n\tSorry,", letter_guess, "is not in the word.\n")
  57.    else:
  58.        print("Please guess only one real letter of the alphabet.")
  59.        
  60. # get the player's guess
  61. guess = input("\nYour guess at the word: ")
  62. if guess != correct:
  63.    print("\n\tSorry, that's not it.")
  64.    print("\nThe word was:", word)
  65.  
  66. # congratulate player
  67. if guess == correct:
  68.    print("\n\tThat's it! You guessed it!\n")
  69.  
  70. # end the game
  71. print("\nThanks for playing.")
Advertisement
Add Comment
Please, Sign In to add comment