Question

In: Computer Science

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 statement to set the secret number to 1458.

Your program should start by welcoming the user and prompting the user to enter a guess:

Welcome to the guessing game. You have ten tries to guess my number.
Please enter your guess:

You can assume that the user will enter an integer as a guess; however, your program must verify that each guess is positive and less than 10000. If it is not, it should print the following message and reprompt the user:

Your guess must be between 0001 and 9999.
Please enter a valid guess:

It should repeat this until the user enters a valid guess.If the user has entered a correct guess, your program should print the message:

That's correct!
Congratulations! You guessed it in X guesses.

where X is the appropriate number of tries. However, if the user guessed correctly on the first try, your program should print the message:

That's correct!
Congratulations! You guessed it on the first try!

Your program should then terminate.If the user's guess is low, your program should print the message:

Your guess is too low.
Guesses so far: X

where X is the number of tries so far.If the guess was the user's tenth guess, your program should print the message:

Game over: you ran out of guesses.

Your program should then terminate.If it was not the tenth guess, then your program should prompt the user for the next guess:

Please enter your guess:

If the user's guess is high, your program should print the message:

Your guess is too high.
Guesses so far: X

where X is the number of tries so far.If the guess was the user's tenth guess, your program should print the message:

Game over: you ran out of guesses.

Your program should then terminate.If it was not the tenth guess, then your program should prompt the user for the next guess:

Please enter your guess:

Expected output:

This is what your output should look like if the user guessed the number on the first try:

Welcome to the guessing game.  You have ten tries to guess my number.
Please enter your guess: 1458
That's correct!
Congratulations! You guessed it on the first try!
>>> 

This is what your output might look like if the user guessed the number within ten tries:

Welcome to the guessing game.  You have ten tries to guess my number.
Please enter your guess: 5000
Your guess is too high.
Guesses so far: 1
Please enter your guess: 2500
Your guess is too high.
Guesses so far: 2
Please enter your guess: 1250
Your guess is too low.
Guesses so far: 3
Please enter your guess: 1800
Your guess is too high.
Guesses so far: 4
Please enter your guess: 1500
Your guess is too high.
Guesses so far: 5
Please enter your guess: 1400
Your guess is too low.
Guesses so far: 6
Please enter your guess: 1450
Your guess is too low.
Guesses so far: 7
Please enter your guess: 1460
Your guess is too high.
Guesses so far: 8
Please enter your guess: 1458
That's correct!
Congratulations! You guessed it in 9 guesses.
>>> 

This is what your output might look like if the user failed to guess the number within ten tries:

Welcome to the guessing game.  You have ten tries to guess my number.
Please enter your guess: 5000
Your guess is too high.
Guesses so far: 1
Please enter your guess: 2500
Your guess is too high.
Guesses so far: 2
Please enter your guess: 1200
Your guess is too low.
Guesses so far: 3
Please enter your guess: 1800
Your guess is too high.
Guesses so far: 4
Please enter your guess: 1500
Your guess is too high.
Guesses so far: 5
Please enter your guess: 1350
Your guess is too low.
Guesses so far: 6
Please enter your guess: 1375
Your guess is too low.
Guesses so far: 7
Please enter your guess: 1400
Your guess is too low.
Guesses so far: 8
Please enter your guess: 1425
Your guess is too low.
Guesses so far: 9
Please enter your guess: 1450
Your guess is too low.
Guesses so far: 10
Game over: you ran out of guesses.
>>> 

This is what your output might look like if the user entered a few invalid guesses:

Welcome to the guessing game.  You have ten tries to guess my number.
Please enter your guess: 20000
Your guess must be between 0001 and 9999.
Please enter a valid guess: 5000
Your guess is too high.
Guesses so far: 1
Please enter your guess: -2500
Your guess must be between 0001 and 9999.
Please enter a valid guess: -1200
Your guess must be between 0001 and 9999.
Please enter a valid guess: 1
Your guess is too low.
Guesses so far: 2
Please enter your guess: 1000
Your guess is too low.
Guesses so far: 3
Please enter your guess: 1400
Your guess is too low.
Guesses so far: 4
Please enter your guess: 1500
Your guess is too high.
Guesses so far: 5
Please enter your guess: 1450
Your guess is too low.
Guesses so far: 6
Please enter your guess: 1460
Your guess is too high.
Guesses so far: 7
Please enter your guess: 1458
That's correct!
Congratulations! You guessed it in 8 guesses.
>>>

Solutions

Expert Solution

#number to be guessed
num=1458

print("Welcome to the guessing game.  You have ten tries to guess my number.")
#input x(guess)
x=int(input("Please enter your guess: "))
#number of guesses 
count=1

while(count<=10):
    
    # case for guess in first attempt
    if x==num and count==1:
        print("That's correct!\nCongratulations! You guessed it on the first try!")
        break
        
    # case for guess in attempt less than or equal to 10
    elif x==num:
        print("That's correct!\nCongratulations! You guessed it in",count,"guesses.")
        break
        
    # case for invalid input
    elif x<=0 or x>=10000:
        print("Your guess must be between 0001 and 9999.")
        x=int(input("Please enter a valid guess: "))
        
    # case for low guess and count
    elif x>num:
        print("Your guess is too high.\nGuesses so far: ",count)
        
        #when 10 guesses are complete
        if count==10:
            print("Game over: you ran out of guesses.")
            break
        x=int(input("Please enter your guess: "))
        count=count+1
        
    # case for low guess and count
    elif x<num:
        print("Your guess is too low.\nGuesses so far: ",count)
        
        #when 10 guesses are complete
        if count==10:
            print("Game over: you ran out of guesses.")
            break
        x=int(input("Please enter your guess: "))
        count=count+1
#end

OUTPUTS:

1

2


Related Solutions

Please use Python 21. Rock, Paper, Scissors Game Write a program that let's the user play...
Please use Python 21. Rock, Paper, Scissors Game Write a program that let's the user play the game of rock, paper, scissors against the computer. The program should work as follows: 1. When the program begins, a random number in the range of 1 through 3 is generated. If the number is 1, then the computer has chosen rock. If the number is 2, then the computer has chosen paper. If the number is 3, then the computer has chosen...
Rock, Paper, Scissors Game Write a Python program rps.py that lets the user play the game...
Rock, Paper, Scissors Game Write a Python program rps.py that lets the user play the game of Rock, Paper, Scissors against the computer. The program should work as follows: You can set these constant global variables at the top outside of your main function definition: COMPUTER_WINS = 1 PLAYER_WINS = 2 TIE = 0 INVALID = 3 ROCK = 1 PAPER = 2 SCISSORS = 3 For this program 1 represents rock, 2 represents paper, and 3 represents scissors. In...
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.
I need to write PYTHON program which is like a guessing game using randint function which...
I need to write PYTHON program which is like a guessing game using randint function which is already done, the user has to guess a number between 1 and 1000 and the game musn't end until you guess the number, if you input a smaller number you must get hints, the same goes if your input is bigger than the wining number , also you must get notified if you repeat a number (example, you pick 1 and in the...
Subjects: Write a Python program that allows users to play the popular rock-paper-scissor game against the...
Subjects: Write a Python program that allows users to play the popular rock-paper-scissor game against the computer multiple times. This program assesses your knowledge of decision structures, repetition structures, and functions. Requirements: 1.Define a function named as play_game. This function receives two parameters representing the player’s and computer’s choices, and it returns an integer representing the game result from the player’s perspective. Specifically, it returns 1 if the player wins and 0 if the player does not win. Hint: you...
I am using C++ Write a program that allows two players to play a game of...
I am using C++ Write a program that allows two players to play a game of tic-tac-toe. Use a two-dimensional char array with three rows and three columns as the game board. Each element of the array should be initialized with an asterisk (*). The program should run a loop that does the following: Displays the contents of the board array. Allows player 1 to select a location on the board for an X. The program should ask the user...
Use Visual Python or equivalent, to write a program that allows the user to observe the...
Use Visual Python or equivalent, to write a program that allows the user to observe the following: Damped and forced harmonic oscillators. The program should be user friendly and have default values for the initial velocities, positions, masses, and spring constants as well as damping constants. Values and frequencies for the forced oscillators should also be given. It should allow the user to input values for the velocities, positions, spring constants, and masses. The program should determine automatically the scale...
Write a program where a user of this program will play a game in which he/she...
Write a program where a user of this program will play a game in which he/she needs to guess a target number, which is a number that the program has randomly picked in the range that the user chooses. The program will repeatedly prompt for the guessed number and provide a clue whether the guessed number is bigger or smaller than the target number, until the guessed number equals the target number.
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!”.
In Python, write a program that allows the user to enter a number between 1-5 and...
In Python, write a program that allows the user to enter a number between 1-5 and returns the vitamins benefits based on what the user entered. The program should: Continue to run until the user quits and Validate the user’s input (the only acceptable input is 0, 1, 2, 3, 4, or 5), If the user enters zero “0” the program should terminate You can use the following facts: 1- Vitamin A protects eyes from night blindness and age-related decline...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT