Skip to content

Instantly share code, notes, and snippets.

@Logic2apply
Created November 18, 2021 13:19
Show Gist options
  • Save Logic2apply/c0ed56138cfcc48d5892fed0cf7bca4d to your computer and use it in GitHub Desktop.
Save Logic2apply/c0ed56138cfcc48d5892fed0cf7bca4d to your computer and use it in GitHub Desktop.

Guess The Number (MultiPlayer)

Problem Statement-

Generate a random integer from a to b. You and your friend have to guess a number between two numbers, a and b. a and b are inputs taken from the user. Your friend is player 1 and plays first. He will have to keep choosing the number, and your program must tell whether the number is greater than the actual number or less than the actual number. Log the number of trials it took your friend to arrive at the number. You play the same game, and then the person with the minimum number of trials wins! Randomly generate a number after taking a and b as input and don’t show that to the user.

python GuessTheNumber.py
import random
class player:
def __init__(self, name, inital, final):
self.name = name
self.initial = inital
self.final = final
self.trials = 0
self.generated_number = random.randint(self.initial, self.final)
def checkCorrection(self, enteredNum):
if self.generated_number == enteredNum:
return (True, "You Guessed Correct Number...!")
elif self.generated_number < enteredNum:
return (False, "Wrong Guess a smaller again...!")
elif self.generated_number > enteredNum:
return (False, "Wrong Guess a bigger number again...!")
def play(self, checkCorrection):
print(f"\nPlease guess a number between {self.initial} and {self.final}\n")
print(f"\n\n{self.name}:")
while True:
numInp = int(input("\nGuess the Number: "))
is_correct = checkCorrection(numInp)[0]
print(checkCorrection(numInp)[1])
if is_correct: break
else: self.trials += 1
if __name__ == "__main__":
print("\nWelcome to:\t\t\tGUESS THE NUMBER\nYou have to guess the number between two given number the player who guessed the number in the least trials will WINS!\n")
try:
initial_num = int(input("Enter the First Number\n: "))
final_num = int(input("Enter the Second Number\n: "))
if initial_num >= final_num:
print("\n\n\tERROR: First number must be smaller than second number.....!!!\n")
initial_num = int(input("Enter the First Number\n: "))
final_num = int(input("Enter the Second Number\n: "))
except Exception as e:
print("\n\n\tERROR: Please Enter Integers Only...!")
player_num = int(input("\nEnter the Number of players who want to play: "))
players = []
for i in range(player_num):
player_name = str(input(f"\n\nEnter Player {i+1} Name: "))
players.append(player_name)
scores = {}
for player_name in players:
playable = player(player_name, initial_num, final_num)
playable.play(playable.checkCorrection)
trialsTaken = playable.trials
scores.update({f"{player_name}":trialsTaken})
print("\n\n\nScores:")
for key, value in scores.items():
print(f"{key} - {value}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment