Above is an example of input for this problem. Write a program (in Python V.2) t
ID: 3602977 • Letter: A
Question
Above is an example of input for this problem.
Write a program (in Python V.2) that is able to pick a random word from a list of words contained in a seperate file (pictured) 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. (as shown in picture)
#A file with words Related to school college classroom degree university graduateExplanation / Answer
Here code is written with proper commenting python3.
name of file is supposed testfile.txt and it should be in same directory
# start of code
#main function
import random # to generate random number
words = '' #declaring and initating it
count = 0 # to count number of words in order to genrate random numbers
turns = 0 # to intiate number of turns used initailly to 0
words_list = [] # for saving words in array
file = open("testfile.txt", "r") # opening File
for line in file: # reading file line by line
if ((line[0] != '#') and (line[0] != ';')): # checking and ignnoring line if file is starting from # or ;
words = line.split() # spliting words
for word in words: # generating loop of words to save
words_list.append(word) # saving in a list
count +=1
name = ''
save = ''
input_list = []
incount =0
flag = 0
name = raw_input("Enter Your Name to Play Game: ")
ran_word = random.randint(1,count) # to generate random number within a range
ran_word = words_list[ran_word]
# print ("random word",ran_word) # just to print the generated random word in case you want to test
print "=====Welcome",name, " To the Game===== "
print "Now you have 9 chances to guess the word. "
save = ''
while incount < 9: # loop to run 9 times
save = raw_input("Enter the word: ") # saving inout
if save in input_list : # not adding chance to attempts if word alread enetered
print ("This word you already tried: ",save)
else : # in case word not tried before
input_list.append(save) # saving it to list
incount += 1 # incrementing
if ( save == ran_word ) : # if input eneterd match
flag = 1
print("Your input is matched: ")
print("You took",incount, " attempts")
else : # in case not matched
print(" Not matched :-( try again")
if (flag == 0) : # if word not enetered after 9 attempts
print ("None of your attempt matched after 9 legal attempts!!!")
file.close() # Closing the file
#End of code
Output of program
dps@Machine:~/Documents/C++$ python filehandlinghpython.py
Enter Your Name to Play Game: dads
=====Welcome dads To the Game=====
Now you have 9 chances to guess the word.
Enter the word: ds
Not matched :-( try again
Enter the word: ds
('This word you already tried: ', 'ds')
Not matched :-( try again
Enter the word: ds
('This word you already tried: ', 'ds')
Not matched :-( try again
Enter the word: ds
('This word you already tried: ', 'ds')
Not matched :-( try again
Enter the word: ds
('This word you already tried: ', 'ds')
Not matched :-( try again
Enter the word: as
Not matched :-( try again
Enter the word: df
Not matched :-( try again
Enter the word: dfs
Not matched :-( try again
Enter the word: asc
Not matched :-( try again
Enter the word: dc
Not matched :-( try again
Enter the word: ad
Not matched :-( try again
Enter the word: fec
Not matched :-( try again
Enter the word: edv
Not matched :-( try again
None of your attempt matched after 9 attempts!!!
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.