Write a program (in Python V.2) that is able to pick a random word from a list o
ID: 3602944 • Letter: W
Question
Write a program (in Python V.2) that is able to pick a random word from a list of words contained in a file and lets the user who is interacting with the game guess it one character at a time.
Upon startup, the player must be prompted for their name.
While playing, the player may guess at most 9 incorrect times. If the player guesses a character that has already been tried, he or she must NOT be penalized for doing so (i.e., it will not count as a turn).
The game ends when the full word has been guessed (player wins) or when nine incorrect guesses have been made.
Upon completion of the game, the player's name, the number of turns played, the word that should have been guessed, and an indicator for win/lose must be added to a file.
Make sure to document your code appropriately.
File Format Specification
The file format is as follows:
- The first non-comment line of the file will contain only a number. That number represents the number of usable words in the file.
- Each word will be on a line by itself. Words may contain only letters (a-z)
- Lines starting with a semicolon (;) or with a hashmark (#) are comments and must be ignored.
Explanation / Answer
import random
tries = 0
print "Welcome to the word game!"
print " I'm going to think of a word and you have to guess it!"
print " Guess which letters are in the word, then you have to guess the whole thing!"
print " Good luck!"
WORDS = ("follow", "waking", "insane", "chilly", "massive",
"ancient", "zebra", "logical", "never", "nice")
word = random.choice(WORDS)
correct = word
length = len(word)
length = str(length)
guess = raw_input("The word is " + length + " letters long. Guess a letter!: ")
while tries < 5:
for guess in word:
if guess not in word:
print "Sorry, try again."
else:
print "Good job! Guess another!"
tries = tries + 1 #*
if tries = 5:
final = raw_input ("Try to guess the word!: ")
if final = correct:
print "Amazing! My word was ", word, "!"
else:
print "Sorry. My word was ", word, ". Better luck next time!"
raw_input(" Press enter to exit")
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.