xxxxxxxxxx
from random import randint
num = int(input("Enter a number = "))
ran = randint(1, 10)
if num == ran:
print("Winner!")
print("You pressed - ", num, "and random number is - ", ran)
xxxxxxxxxx
from random import randint
from pcinput import getInteger
answer = randint(1, 200)
count = 0
while True:
guess = getInteger("What is your estimate? ")
if guess < 1 or guess > 200:
print("Your estimate must be between 1 and 200")
continue
count += 1
if guess < answer:
print("Higher")
elif guess > answer:
print("lower")
else:
print("You guessed it!")
break
if count == 1:
print("Wow first try !!.")
else:
print("You estimated it in", count, "times.")
xxxxxxxxxx
import random
def instructions():
print("Welcome to the number guessing game.")
print("Guess a number between 1 and 10.")
print("You only have 3 guesses.")
def game():
# Guess limit so the user can only guess three times
guess_limit = 1
# The random guess
actual_number = random.randint(1, 10)
# What user can type and see
guessed_number = int(input("What is the number?: "))
# In case you guessed it right at the first time
if actual_number == guessed_number:
print("You guessed it right! The number is ", actual_number)
# The while loop so it can go on
while guessed_number != actual_number:
if guessed_number > actual_number:
print("Lower")
elif guessed_number < actual_number:
print("Higher")
guessed_number = int(input("What is the number?: "))
guess_limit += 1
if guess_limit == 3 and guessed_number != actual_number:
print("You ran out of guess, The answer was number ", actual_number)
break
else:
print("You guessed it right! The number is ", actual_number)
instructions()
game()
xxxxxxxxxx
import random
num = random.randint(1, 9)
while True:
try:
guess = int(input("\nPlease enter a guess from 1-9: "))
if 0 < guess < 10:
if guess > num:
print("\nYou guessed too high")
elif guess < num:
print("\nYou guessed too low")
elif guess == num:
print("\nYou guessed correctly")
while True:
u_input = input("\nWould you like to play again? y/n: ")
if u_input == 'n':
exit()
elif u_input == 'y':
num = random.randint(1, 9)
break
elif u_input != 'y' and u_input != 'n':
print("\nError: Please select a valid option")
elif guess < 1 or guess > 9:
print("\nError: Please enter a number from 1-9")
except ValueError:
print("\nError: Please enter a number")
xxxxxxxxxx
import random
import os
os.system('cls') # Enter CLS if You're on Windows, or CLR instead of CLS if You're on MacOS or Linux
print("Welcome to Guess The Number.")
number_range = int(input("Please Enter a Number to Guess Between > "))
tries = int(input("Please Enter How Many Tries You'd Like. > "))
number = random.randint(0, number_range)
guessed = False
lost = False
tried = 0
while guessed == False and lost == False:
os.system('cls')
guess = int(input("Try and Guess The Number. > "))
os.system('cls')
if guess > number:
print("Too High! Try to Go Lower.")
input()
elif guess < number:
print("Too Low! Try to Go Higher.")
input()
elif guess == number:
print("You Guessed The Number! You Won!")
input()
guessed = True
tried += 1
if tried == tries:
os.system('cls')
print(f"Aww. You Ran out of Tries. {number} was The Answer.")
lost = True
if lost == False:
os.system('cls')
print("Congratulations! You Won Guess The Number.")
if lost == False:
print("Congratulations! You Won Guess The Number.")
xxxxxxxxxx
import random
def guess_number():
# Generate a random number between 1 and 100
secret_number = random.randint(1, 100)
# Initialize the number of attempts
attempts = 0
print("Welcome to the Number Guessing Game!")
print("I'm thinking of a number between 1 and 100.")
while True:
# Get user's guess
guess = int(input("Enter your guess: "))
# Increment the number of attempts
attempts += 1
# Check if the guess is correct
if guess == secret_number:
print(f"Congratulations! You've guessed the number in {attempts} attempts.")
break
elif guess < secret_number:
print("Too low! Try guessing a higher number.")
else:
print("Too high! Try guessing a lower number.")
# Call the function to start the game
guess_number()