Problem Statement

The game will select two random numbers from to , one for each player. Then, each player will guess a number in the range, and the game will print either “higher” if that player’s secret number is larger than the guess, or “lower” if the player’s secret number is smaller than the guess. Players will alternate turns until one player correctly guesses their secret number and wins the game.

Random Numbers

import random

Random Numbers

import random

a = int(input("Enter a minimum value: ")) b = int(input("Enter a maximum value: ")) x = random.randint(a, b) print(fA random number between {a} and {b} is {x}")
import random

p1_secret = random.randint(0, 100) p2_secret = random.randint(0, 100)
import random

p1_secret = random.randint(0, 100) p2_secret = random.randint(0, 100) player = 1 while( ): # player has not guessed correctly # swap player # get new guess from player # check if secret is higher or lower

Boolean Expression

not ((player == 1 and guess == p1_secret) or 
            (player == 2 and guess == p2_secret))

Handling Input

x = int(input(f"Enter a guess for player {player}: "))
while x < 0 or x > 100:
    print("Invalid Input!")
    x = int(input(f"Enter a guess for player {player}: "))

Main Code

p1_secret = random.randint(0, 100)
p2_secret = random.randint(0, 100)
player = 1
while(not ((player == 1 and guess == p1_secret) or 
        (player == 2 and guess == p2_secret))): 
    # player has not guessed correctly
    # swap player
    x = int(input(f"Enter a guess for player {player}: "))
    while x < 0 or x > 100:
        print("Invalid Input!")
        x = int(input(f"Enter a guess for player {player}: "))
    guess = x
    # check if secret is higher or lower

Main Code

p1_secret = random.randint(0, 100)
p2_secret = random.randint(0, 100)
player = 1
while(not ((player == 1 and guess == p1_secret) or 
        (player == 2 and guess == p2_secret))): 
    # player has not guessed correctly
    if player == 1:
        player = 2
    else:
        player = 1
    x = int(input(f"Enter a guess for player {player}: "))
    while x < 0 or x > 100:
        print("Invalid Input!")
        x = int(input(f"Enter a guess for player {player}: "))
    guess = x
    # check if secret is higher or lower

Branches

if player == 1:
    if guess < p1_secret:
        print("Higher")
    elif guess > p1_secret:
        print("Lower")
    else:
        print("Correct!")
else:
    if guess < p2_secret:
        print("Higher")
    elif guess > p2_secret:
        print("Lower")
    else:
        print("Correct!")
import random

p1_secret = random.randint(0, 100) p2_secret = random.randint(0, 100) player = 1 while(not ((player == 1 and guess == p1_secret) or (player == 2 and guess == p2_secret))): # player has not guessed correctly if player == 1: player = 2 else: player = 1 x = int(input(f"Enter a guess for player {player}: ")) while x < 0 or x > 100: print("Invalid Input!") x = int(input(f"Enter a guess for player {player}: ")) guess = x if player == 1: if guess < p1_secret: print("Higher") elif guess > p1_secret: print("Lower") else: print("Correct!") else: if guess < p2_secret: print("Higher") elif guess > p2_secret: print("Lower") else: print("Correct!")
import random

p1_secret = random.randint(0, 100) p2_secret = random.randint(0, 100) player = 2 guess = -1 while(not ((player == 1 and guess == p1_secret) or (player == 2 and guess == p2_secret))): # player has not guessed correctly if player == 1: player = 2 else: player = 1 x = int(input(f"Enter a guess for player {player}: ")) while x < 0 or x > 100: print("Invalid Input!") x = int(input(f"Enter a guess for player {player}: ")) guess = x if player == 1: if guess < p1_secret: print("Higher") elif guess > p1_secret: print("Lower") else: print("Correct!") else: if guess < p2_secret: print("Higher") elif guess > p2_secret: print("Lower") else: print("Correct!")
import random

p1_secret = random.randint(0, 100) p2_secret = random.randint(0, 100) player = 2 guess = -1 while(not ((player == 1 and guess == p1_secret) or (player == 2 and guess == p2_secret))): # player has not guessed correctly if player == 1: player = 2 else: player = 1 x = int(input(f"Enter a guess for player {player}: ")) while x < 0 or x > 100: print("Invalid Input!") x = int(input(f"Enter a guess for player {player}: ")) guess = x if player == 1: if guess < p1_secret: print("Higher") elif guess > p1_secret: print("Lower") else: print("Correct!") else: if guess < p2_secret: print("Higher") elif guess > p2_secret: print("Lower") else: print("Correct!")