35 lines
968 B
Python
35 lines
968 B
Python
import random
|
|
|
|
# Generate a random number between 1 and 100
|
|
secret_number = random.randint(1, 100)
|
|
print("Welcome to the Two-Player Number Guessing Game!")
|
|
print("I'm thinking of a number between 1 and 100.")
|
|
|
|
player1_name = input("Player 1, enter your name: ")
|
|
player2_name = input("Player 2, enter your name: ")
|
|
|
|
# Set the number of attempts
|
|
attempt = 0
|
|
|
|
while True:
|
|
attempt += 1
|
|
# Players take turns guessing
|
|
if attempt % 2 != 0:
|
|
print(f"{player1_name}'s turn.")
|
|
else:
|
|
print(f"{player2_name}'s turn.")
|
|
|
|
guess = int(input("Make a guess: "))
|
|
|
|
if guess < secret_number:
|
|
print("Too low. Try again.")
|
|
elif guess > secret_number:
|
|
print("Too high. Try again.")
|
|
else:
|
|
if attempt % 2 != 0:
|
|
winner = player1_name
|
|
else:
|
|
winner = player2_name
|
|
print(f"Congratulations, {winner}! You have guessed the number correctly. It was {secret_number}.")
|
|
break
|