Guess the number (High / low game) - Python tutorial
How to create a 'guess the number' (higher / low) game in Python (Free python web tutorial).
Introduction:
How the game works
The computer will think of a random number between 1 and 20, and ask the player to guess it. The computer will inform the player whether each guess is too high or too low. The player wins if they can guess the number within six attempts.
How the game works
The computer will think of a random number between 1 and 20, and ask the player to guess it. The computer will inform the player whether each guess is too high or too low. The player wins if they can guess the number within six attempts.
In this tutorial, you will learn how to create your very own 'guess the number' game using Python.
Part 1: Higher or lower?
Before creating your guess the number game, let's explore some of the functions you will be using.
STEP 1
Let's start by writing a program to compare two numbers and return a value 'higher' or 'lower' for the second number.
Type in and run the following code:
Before creating your guess the number game, let's explore some of the functions you will be using.
STEP 1
Let's start by writing a program to compare two numbers and return a value 'higher' or 'lower' for the second number.
Type in and run the following code:
Num1 = 5
Num2 = 10 if Num2 > Num1: print("Higher") else: print("Lower") |
Challenge 1
- Try changing Num1 to 10 and run the code again to see what happens.
Challenge 2
There is a problem in our code. If Num1 is the same as Num2, the program will display the message 'Lower'.
- Modify your program so that it displays the message 'Same' if both numbers are the same.
Hint: You will need to use an 'elif' statement.
|
Part 2: Creating a dice simulator
STEP 1
Let's start by writing a program to simulate a roll of a dice. This will form the basis of your 'guess the number' game.
Type in and run the following code:
STEP 1
Let's start by writing a program to simulate a roll of a dice. This will form the basis of your 'guess the number' game.
Type in and run the following code:
import random
number = random.randint(0,3) print(number) |
Make sure you comment your code, using the hashtag (#), explaining what the code is doing.
Challenge 1
Modify the code above so that it picks a number between 1 and 6.
Challenge 1
Modify the code above so that it picks a number between 1 and 6.
Run the new script and explain what the new code does.
Part 3: Creating the 'guess the number' game.
You now have all the components you need to create your very own guess the number game. Enter the following sample code into the code window below.
You now have all the components you need to create your very own guess the number game. Enter the following sample code into the code window below.
import random
number = random.randint(1, 10) player_name = input("Hello, what's your name?") number_of_guesses = 0 print("Okay " + player_name + ", I am Guessing a number between 1 and 10:") while number_of_guesses < 5: guess = int(input("What is your guess? ")) number_of_guesses += 1 if guess < number: print("Your guess is too low") elif guess > number: print("Your guess is too high") else: break if guess == number: print("Congratulations! You guessed correctly.") else: print("You did not guess the number, the number was ") print(number) |
Example Solutions
guess_my_number_solution.zip |
Challenge:
- Modify you code so that it tells the player how many guesses they made.
- Change the random number from 1-10 to 1 - 100 (Remember to change the message at the start of the game!).
- Keep looping the game until the player quits and keep a track of the player's best score (least number of guesses).
You may also be interested in:
Licence:
Unless otherwise specified, everything in this repository is covered by the following licence:
Unless otherwise specified, everything in this repository is covered by the following licence:
Tags: programming language python, python programming language, python coding language, programming languages python, python code language, python programming language example, what is the python programming language, python scripting, python language tutorial, what type of programming language is Python, python class, python how to learn, learn python, learn python from scratch, where to learn Python, coding on computer, computer coding, best place to learn coding online, online coding, online code, computer programming and Coding, hour of code program, computer science degree programs, programs for computer science, what does coding mean in computers, how does computer code work, what is computer programmer.