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...
USING PYTHON. Thank you in advance Write a program that allows the user to enter a...
USING PYTHON. Thank you in advance Write a program that allows the user to enter a series of string values into a list. When the user enters the string ‘done’, stop prompting for values. Once the user is done entering strings, create a new list containing a palindrome by combining the original list with the content of the original list in a reversed order. Sample interaction: Enter string: My Enter string: name Enter string: is Enter string: Sue Enter string:...
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...
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...
Write a program that allows 2 players to play the marble game using combinations of loop...
Write a program that allows 2 players to play the marble game using combinations of loop and conditional statements. In the marble game, you ask the user to enter how many marbles to start with. Then, the game begins. The first player must take 1, 2 or 3 marbles. Then the second player goes and must take 1, 2 or 3 marbles. The winner is the player who takes the last marble. Allow two users to play this game and...
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...
Write in Python and as 2 seperate programs Write a program that allows the user to...
Write in Python and as 2 seperate programs Write a program that allows the user to enter the total rainfall for each of 12 months into a LIST. The program should calculate and display the total rainfall for the year, the average monthly rainfall, and the months with the highest and lowest rainfall amounts. Data: January 7.9 inches February 10.1 inches March 3.4 inches April 6.7 inches May 8.9 inches June 9.4 inches July 5.9 inches August 4.1 inches September...
How to do this in Python (using Lists): Create a python program that allows a user...
How to do this in Python (using Lists): Create a python program that allows a user to display, sort and update as needed a List of U.S States containing the State Capital and State Bird. You will need to embed the State data into your Python code. The user interface will allow the user to perform the following functions: 1. Display all U.S. States in Alphabetical order along with Capital and Bird 2. Search for a specific state and display...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT