Write a Python program that simulates a guessing game. The program should be structured as follows:1. Define a function called guessing_game() that implements the game logic.2. Inside the guessing_game() function:· Generate a random number between 1 and 100 as the secret number.· Prompt the user to guess the secret number within ten attempts; this should be done using a while loop.· Provide hints to the user after each guess, indicating whether the guess is too high or too low.· Count the number of guesses made by the user.· If the user correctly guesses the number within ten attempts, print a congratulatory message along with the number of attempts.· If the user exceeds the maximum number of guesses without guessing the correct number, print a message informing them that they have run out of guesses and reveal the correct number.3. Call the guessing_game() function to start the game.
Question
Write a Python program that simulates a guessing game. The program should be structured as follows:1. Define a function called guessing_game() that implements the game logic.2. Inside the guessing_game() function:· Generate a random number between 1 and 100 as the secret number.· Prompt the user to guess the secret number within ten attempts; this should be done using a while loop.· Provide hints to the user after each guess, indicating whether the guess is too high or too low.· Count the number of guesses made by the user.· If the user correctly guesses the number within ten attempts, print a congratulatory message along with the number of attempts.· If the user exceeds the maximum number of guesses without guessing the correct number, print a message informing them that they have run out of guesses and reveal the correct number.3. Call the guessing_game() function to start the game.
Solution
Here is a Python program that implements the guessing game as described:
import random
def guessing_game():
secret_number = random.randint(1, 100)
attempts = 0
while attempts < 10:
guess = int(input("Guess the secret number (between 1 and 100): "))
attempts += 1
if guess < secret_number:
print("Too low!")
elif guess > secret_number:
print("Too high!")
else:
print(f"Congratulations! You found the secret number in {attempts} attempts.")
return
print(f"You have run out of guesses. The secret number was {secret_number}.")
guessing_game()
This program first generates a random number between 1 and 100. Then it enters a while loop where it prompts the user to guess the number. If the guess is too low, it informs the user that the guess is too low. If the guess is too high, it informs the user that the guess is too high. If the guess is correct, it congratulates the user and exits the function. If the user runs out of guesses, it informs the user and reveals the secret number.
Similar Questions
For this assignment you will write the first part of a program that will play a simple "Guess the Number" game. In the full version of this game (which you will write for this week's project) the program will randomly generate an integer between 1 and 200 inclusive and then will repeatedly ask the user for a guess. If they guess a number that is less than 1 or more than 200 an error message will be printed as a part of this loop. For this lab you will write the piece of the code that checks for this error and ends if the user picks the right number.Sample Output This is a sample transcript of what your program should do. Items in bold are user input and should not be put on the screen by your program. Note that the code for this lab includes a "debugging" output that starts with the word "DEBUG: " that tells you the number that was randomly chosen. In the full version of this program that will be removed, but for this incremental piece your lab submission should include it. (You should get used to having this kind of debugging output in your code that isn't part of the final program but is crucial to ensuring that each incremental piece is working properly before you move onto the next part of the program).If the user enters an invalid choice, your code should inform them that their choice was invalid and ask them to guess again.Enter a random seed: 99DEBUG: The number picked is: 188Enter a guess between 1 and 200: 259Your guess is out of range. Pick a number between 1 and 200.That is not the number.Enter a guess between 1 and 200: -1Your guess is out of range. Pick a number between 1 and 200.That is not the number.Enter a guess between 1 and 200: 88That is not the number.Enter a guess between 1 and 200: 188Congratulations! Your guess was correct!I had chosen 188 as the target number.You guessed it in 4 tries.Random numbers: Just like in the FunWithBranching programming assignment, you must use the Random class to generate numbers between 1 and 200. Make sure you are using int values and not doubles for this assignment! Look back at your submitted code for that project and reuse what you can here - reusing code from old assignments isn't only allowed, it's ENCOURAGED! If you've solved a problem once you should remind yourself how you solved it when asked to do something similar!
Write a two player guessing game. Players must guess a secret number (an integer) which is given as a command line argument.The program will begin by asking players for their name.The program will alternate, asking each player in the following style: <player>, what is your guess? When the player inputs a guess, 3 possible outcomes are possible.The secret number is higher than the player's guess. The program will then output Higher and go to the next player.The secret number is lower than the player's guess. The program will then output Lower and go to the next player.The secret number is equal to the player's guess. The program will print <player> wins! and the program will end.Assume all inputs are valid
The "Masked Gun" game is a simple guessing game where the player attempts to unlock a masked gun by correctly guessing a secret 4-digit code.Arrange the steps in the correct sequence to create a Python code for a game.1) Implement Game Loop: - Set up a loop for the main game logic.2) Display Masked Gun: - Create a function to visually represent the masked gun.3) Generate Secret Code - Write a function to generate a random 4-digit secret code.4) Provide Feedback and Manage Attempts for Guessing - Display feedback based on the correctness of the guess and keep track of the number of attempts left for guessing5) End Game: - End the game when the player correctly guesses the code or runs out of attempts.6) Validate and Compare Guess with Secret Code - Check if the entered guess is a 4-digit numeric code and compare the player's guess with the generated secret code.Enter your answer as a sequence without commas (NAT):
can you make a program of game which tells the score when given a choice like a and b where a and b are famous celebrities or influencers. we have to guess out of a and b which has more followers. a and b are randomly generated from already prepared people list which has there name, follower count, description and country (we must display name, description and country). the game continues until a wrong choice is guessed. with every guess it must ask the user again to guess who has more instagram followers and clear the screen after the answer is given untill the user enters the wrong answer the screen must be cleared and said sorry you lost and print there score final . i will provide the peoples list
import randomnumerate = [input (answer)]def Random_Choice_out_of_what_you_want ():question = input("Say a prayer for your fortune... ")answer = random.choice(input, numerate)print(answer)Random_Choice_out_of_what_you_want ():
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.