Number Guessing Game

Lesson 4 — Build and run this in Python (online-python.com)

Project Overview

The computer chooses a random number and the player keeps guessing until they find it. This practices loops and conditionals.

Full Python Code

import random

number = random.randint(1, 20)
tries = 0

print("I'm thinking of a number between 1 and 20.")

while True:
    guess = int(input('Take a guess: '))
    tries += 1
    if guess < number:
        print('Too low.')
    elif guess > number:
        print('Too high.')
    else:
        print('Correct! You guessed in', tries, 'tries.')
        break
Open online-python.com and paste code

Extension

Limit the number of tries and give the player a score. Or let them choose the difficulty range.