Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

number guessing game python

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.")
Comment

python number guessing game

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()
Comment

python random number guessing game

'''
Note that if the player types something other than a number, the
program will crash. If you want to fix this, use try and except to
catch the error and tell the player it was invalid.
'''

import random

number = random.randint(1, 100) # Generate random number
num_of_guesses = 0 # Number of guesses player took
guess = 0 # Player's current guess

# Introduce player to game
print('Welcome to my number guessing game!')
print('I have come up with a number between 1 and 100, guess what it is.')

running = True
while running:
  
  guess = int(input('Guess: '))
  num_of_guesses += 1
  
  if guess > number:
    print('Too high, try again.')
  elif guess < number:
    print('Too low, try again.')
  else:
    print('Good job!')
    print(f'It only took you {num_of_guesses} tries')
    running = False # Exits out of while loop
    
print('Thanks for playing!')
Comment

PREVIOUS NEXT
Code Example
Python :: feature to determine image too dark opencv 
Python :: timer 1hr 
Python :: dataframe to dictionary using index as key 
Python :: how to unlist a list in python 
Python :: pandas data frame to list 
Python :: relativefrequencies of the unique values pandas 
Python :: how to play and stop music python 
Python :: python argparse optional required 
Python :: how to add attribute to class python 
Python :: pip install django celery results 
Python :: python sum of list 
Python :: python practice 
Python :: python formdata requests 
Python :: subarray in python 
Python :: change item in list python 
Python :: input in one line python 
Python :: get last 3 things in a list python 
Python :: iterate over dataframe 
Python :: enumerate string pythonm 
Python :: python iterate through objects attributes 
Python :: sort folders content by name python 
Python :: convert to datetime object 
Python :: flask error handling 
Python :: how to stop a program after 1 second in python 
Python :: Program for length of the shortest word 
Python :: pygame mixer documentation 
Python :: how to set and run flask app on terminal 
Python :: quick sort python 
Python :: most popular python libraries 
Python :: ffmpeg python video from images 
ADD CONTENT
Topic
Content
Source link
Name
3+5 =