Question

In: Computer Science

Python3  Please add comments Write a program that implements the word guessing game - There is a...

Python3  Please add comments

Write a program that implements the word guessing game

- There is a secret word

- The user enters letters one at a time, if the letter appears in the secret word, it is revealed. Otherwise, it counts as a wrong guess.

If the user reveals all the letters in the word before getting too many wrong guesses then they win!

Otherwise, they lose.

1 - define secret word

2 - create a revealed letter list that starts empty

2. set wrong guess count to 0

3. ask the user for a letter

4. is the letter in the secret word?

5. if so: add it to the revealed letters list

create a blank string

for each letter in the secret word, if the letter in

revealed letter list, add that letter to string,

otherwise add an underscore to string

6. if letter not in secret word, add one to wrong guess

7. repeat from step 3

Solutions

Expert Solution

NOTE: change the secret word and guess limit as per your requirements.

PYTHON CODE:

# secret word
secret_word='python'

# list to store the revealed letter
revealed_letter=[]

# count for wrong guess
wrong_guess=0

# limit for guess
GUESS_LIMIT =6

# infinite loop
while True:

    # getting a letter from the user
    letter=input('Enter a letter: ')

    # changing to lower case
    letter=letter.lower()

    # checking the letter in secret word
    if letter in secret_word:

        # if found, it is added to the revealed_letter list
        revealed_letter.append(letter)

    # empty string variable
    word=''

    # for every letter in the secret word
    for i in secret_word:

        # if the letter in revealed_letter list, then it is added
        # to the string variable 'word'
        if i in revealed_letter:
            word+=i
        else:

            # if not found, '_' is added to the variable
            word+='_'

    # if the user input letter not found in secret word
    # increment the wrong_guess count
    if letter not in secret_word:      
        wrong_guess+=1

    # print the user guessed letter
    print(word+'\n')

    # deciding the win or lost
    if word != secret_word and wrong_guess >= GUESS_LIMIT:
        print('You lost!')
        break
    elif word == secret_word and wrong_guess < GUESS_LIMIT:
        print('You win!')
        break
    else:
        pass
  

      
SCREENSHOT FOR CODING:

SCREENSHOT FOR OUTPUT:

     


Related Solutions

Number guessing Game (20 Marks) Write a C program that implements the “guess my number” game....
Number guessing Game Write a C program that implements the “guess my number” game. The computer chooses a random number using the following random generator function srand(time(NULL)); int r = rand() % 100 + 1; that creates a random number between 1 and 100 and puts it in the variable r. (Note that you have to include <time.h>) Then it asks the user to make a guess. Each time the user makes a guess, the program tells the user if...
C++ The program implements the Number Guessing Game. However, in that program, the user is given...
C++ The program implements the Number Guessing Game. However, in that program, the user is given as many tries as needed to guess the correct number. Rewrite the program so that the user has no more than five tries to guess the correct number. Your program should print an appropriate message, such as “You win!” or “You lose!”.
Write a Java program that implements the Number Guessing Game: 1. First generate a random number...
Write a Java program that implements the Number Guessing Game: 1. First generate a random number (int) between 0 and 100, call it N 2. Read user input (a guess) 3. check the number, if it's smaller than N, output "The number is larger than that" 4. If the input is larger than N, output "The number is smaller than that" 5. If the input is equal to N, output " You got it!", and exit 6. Repeat until the...
Random Number Guessing Game (python) *use comments Write a program guess.py that generates a random number...
Random Number Guessing Game (python) *use comments Write a program guess.py that generates a random number in the range of 1 through 20, and asks the user to guess what the number is. If the user's guess is higher than the random number, the program should display "Too high, try again." If the user's guess is lower than the random number, the program should display "Too low, try again." If the user guesses the number, the application should congratulate the...
Write a program using Python that allows the user to play a guessing game (please provide...
Write a program using Python that allows the user to play a guessing game (please provide a picture of code so it is easier to read). The game will choose a "secret number", a positive integer less than 10000. The user has 10 tries to guess the number. Requirements: Normally, we would have the program select a random number as the "secret number". However, for the purpose of testing your program (as well as grading it), simply use an assignment...
IN C++ Write a program to play the Card Guessing Game. Your program must give the...
IN C++ Write a program to play the Card Guessing Game. Your program must give the user the following choices: - Guess only the face value of the card. -Guess only the suit of the card. -Guess both the face value and suit of the card. Before the start of the game, create a deck of cards. Before each guess, use the function random_shuffle to randomly shuffle the deck.
Python3 Write a program to implement the Pokemon game should ask the user to enter their...
Python3 Write a program to implement the Pokemon game should ask the user to enter their type and compare it with a randomly chosen computer type and correctly display who wins/ties.
Write a program in C that implements Conway's Game of Life. You will be expected to...
Write a program in C that implements Conway's Game of Life. You will be expected to apply the principles of Design by Contract to your code. The rules for the Game of Life are simple. The universe consists of a two-dimensional matrix of cells with each cell being alive or dead. For each generation every cell determines its next phase of life as follows: If the cell is alive: it dies if it has 0, 1, 4 or more living...
The first program is to count the number of zeros in $3855. Please     add comments...
The first program is to count the number of zeros in $3855. Please     add comments to each line                 org   $1000 array     db $38, $55 ; data to be tested                      org $1100 zero_cnt ds.b   1 lp_cnt     ds.b   1              org   $1500              clr   zero_cnt       ;initialize the 0 count to 0              movb #16,lp_cnt                   ldd   array         again     lsrd             bcs   chk_end         ;             inc   zero_cnt chk_end   dec   lp_cnt                           bne   again                               swi                          end
Write a guessing game java program (basic program, we didn't study further than loops and files)...
Write a guessing game java program (basic program, we didn't study further than loops and files) where the user has to guess a secret number between 1 and 100. After every guess the program tells the user whether their number was too large or too small. At the end the number of tries needed should be printed. It counts only as one try if they input the same number multiple times consecutively.Example of running this program: Enter your secret number...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT