63.0k views
1 vote
Pick a number

You will write a program that will simulate the game where two players try to guess a number and the one who's closest wins.
1. Random secret number from 1 to 10 or any larger range if you want.
2. Two players will guess
3. Determine who was closest.
4. Display the results of the round in a neat and organized fashion.
5. Add some kind of style to your print outs so that your display is nice

1 Answer

7 votes

In python 3+:

import random

secret_number = random.randint(1,10)

guess_one = int(input("Enter player one's guess: "))

guess_two = int(input("Enter player two's guess: "))

if abs(guess_one - secret_number) < abs(guess_two - secret_number):

print(f"The secret number is {secret_number} and player 1 was closest")

elif abs(guess_one - secret_number) > abs(guess_two - secret_number):

print(f"The secret number is {secret_number} and player 2 was closest")

else:

print(f"The secret number is {secret_number} and it was a tie")

I hope this helps!

User Estephanie
by
4.3k points