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
Question
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
Solution
Here is a simple Python script for a two-player guessing game:
import sys
def guessing_game(secret_number):
player1 = input("Player 1, what is your name? ")
player2 = input("Player 2, what is your name? ")
players = [player1, player2]
guess = None
current_player = 0
while guess != secret_number:
guess = int(input(f"{players[current_player]}, what is your guess? "))
if guess < secret_number:
print("Higher")
elif guess > secret_number:
print("Lower")
current_player = (current_player + 1) % 2
print(f"{players[(current_player + 1) % 2]} wins!")
if __name__ == "__main__":
secret_number = int(sys.argv[1])
guessing_game(secret_number)
To run this script, you would save it to a file (for example, guessing_game.py), and then run it from the command line with the secret number as an argument, like so: python guessing_game.py 42.
This script first asks each player for their name, and then enters a loop where it alternates between players, asking for their guess. If the guess is too low, it prints "Higher", if it's too high, it prints "Lower". If the guess is correct, it prints "<player> wins!" and the program ends.
Similar Questions
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.
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!
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
Alice and Bob are playing a game where they each choose a number, and the goal is to see whose number is closer to 50. Write a program that takes two numbers as input and determines which number is closer to 50. If the numbers are equally close to 50, the program should output 0.Company Tags: CapgeminiInput format :The input consists of two integers: the first integer is Alice's number, and the second integer is Bob's number.Output format :The output prints the number closer to 50. If both numbers are equally close to 50, print 0.Refer to the sample output for formatting specifications.Code constraints :1 ≤ input numbers ≤ 100
There are 8 players. Each player chooses one of the numbers 2, 3, 4, 5, 6, 7, 8, 9 or 10. The player or players whose number(s) is (are) closest to twice of the average of all numbers chosen wins. The payoff of each player is 1 if she wins (regardless of whether or not there are other winners), and 0 if she loses.What is true in this guessing game? None of the other alternatives is true. For each player, choosing the number 2 is the only strategy that survives iterated elimination of strongly dominated strategies. For each player, choosing the number 10 is the strongly dominant strategy. No player has a strongly dominant strategy. For each player, choosing the number 10 is the only strategy that survives iterated elimination of strongly dominated strategies. Every player has a strongly dominated strategy.
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.