Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # word_guess.py
- # Guess The Word
- #
- # The computer picks a random word and the player has to guess the word.
- # The computer tells the player how many letters are in the word.
- # The player has 5 chances to ask if a letter is in the word.f
- # The player must then guess the word.
- #
- # Heidi Heffelfinger
- # November 11, 2012
- # Programming Python for the Absolute Beginner 3rd Ed. Chapter 4
- # Challenge 4
- import random
- # create a sequence of words to choose from; make it a constant
- WORDS = ("adult", "knife", "easy", "magnet", "answer", "penal)
- # pick one word randomly from the sequence
- word = random.choice(WORDS)
- # create a variable to use later to see if the guess is correct
- correct = word
- # find length of the word
- letters = len(word)
- # start the game
- print(
- """
- Welcome to Guess the Word!
- Ask which letters are in a randomly chosen word
- You have 5 chances to ask which letters are in the word.
- You then guess the word.
- (Press the enter key at the prompt to quit.)
- """
- )
- print("I'm thinking of a word.")
- print("There are,", letters, "letters in the word.\n")
- #initialize variables for while loop
- counter = 0
- letter_guess_length = 1
- # player asks about which letters are in the word
- while counter < 5:
- if letter_guess_length == 1:
- letter_guess = input("Ask about a letter in the word: ")
- letter_guess_length = len(letter_guess)
- letter_guess = letter_guess.lower()
- counter += 1
- if letter_guess in word:
- print("\n\tYes,", letter_guess, "is in the word.")
- else:
- print("\n\tSorry,", letter_guess, "is not in the word.\n")
- else:
- print("Please guess only one real letter of the alphabet.")
- # get the player's guess
- guess = input("\nYour guess at the word: ")
- if guess != correct:
- print("\n\tSorry, that's not it.")
- print("\nThe word was:", word)
- # congratulate player
- if guess == correct:
- print("\n\tThat's it! You guessed it!\n")
- # end the game
- print("\nThanks for playing.")
Advertisement
Add Comment
Please, Sign In to add comment