In this article will help the reader use the learning from all the previous blogs to make a mini-project. You’ll discover Python applications in practice. We will be using Visual Studio Code as our code editor. If you have not installed Visual Studio Code, the instructions are given in the first blog.

Python applications in practice – creating a guessing numbers game

This mini-project will be exciting to learn on how we can use functions and most of the other things which we learned in the previous blogs. This mini-project game generates a random number from 1 to 1000 or if you want it to be easy you can decrease the range and the user who is playing the game must guess the number. Sounds exciting, doesn’t it? What will make it more exciting is that we can give the user some cues if he guesses the number wrong so that they can guess the number correctly.

Let’s make a blueprint for the game with Python applications in practice.

Python_applications

Intro command line

In the intro command line, we will ask the user to guess a number. We will ask his name and age. Then we will ask him if he wants to play the game or not. Let’s do this in the code.

# Intro Panel Command line
 
print("Welcome to the guessnum")
 
name=input("what is your name?")
print(f"Hello {name}")
Output:
Welcome to the guessnum
Hello john

As can be seen we first introduced our game to the user and then we asked the user their name. we greeted them using the saved name. Now let’s ask the user the age.

# Intro Panel Command line
 
print("Welcome to the guessnum")
 
name=input("what is your name?")
age=int(input(f"Hello {name}, what is your age?"))
print(f"Hello {name}")
Output:
Welcome to the guessnum
Hello john

In here we are seeing fstring, this is alternative to format, if we write f followed by a string, we can use our stored variables inside the “{}” directly.

Now we can see most of the intro panel. Now let’s ask the user if he wants to play the game and if he wants to play the game, lets ask him to guess a number and we can say if its right or not. But before asking the user to guess the number, we must have the number of the program ready. Let’s see how it is done in code.

# Intro Panel Command line
 
print("Welcome to the guessnum")
 
name=input("what is your name?")
age=int(input(f"Hello {name}, what is your age?"))
choice=input(f"Hello {name}, would you like to play the game? y/n")
 
if choice=="y":
    pass
else:
    print("exiting")
    exit
 

Now we are making another prompt which will ask the user, whether he wants to play the game, and we will be using the conditionals which we learned in the previous blogs to continue if he says yes and if it’s no, to exit the game. Now let’s continue expanding our game and ask the user for the number, but before that let’s make our code select a random number.

# Intro Panel Command line
import random
print("Welcome to the guessnum")
 
name=input("what is your name?")
age=int(input(f"Hello {name}, what is your age?"))
choice=input(f"Hello {name}, would you like to play the game? y/n")
 
if choice=="y":
    number=int(random.randint(1,5))
    guess=int(input("Please input your guess"))
    print(f"your guess is {guess}")
else:
    print("exiting")
    exit
 
 
Output:
Welcome to the guessnum
your guess is 2

Now we added an import known as random which selects a random number from a given range. The function is random.randint(start,end). Then we are asking our user to guess the number and we are printing our users guess.

Let’s also print our program’s guess.

# Intro Panel Command line
import random
print("Welcome to the guessnum")
 
name=input("what is your name?")
age=int(input(f"Hello {name}, what is your age?"))
choice=input(f"Hello {name}, would you like to play the game? y/n")
 
if choice=="y":
    number=int(random.randint(1,5))
    guess=int(input("Please input your guess"))
    print(f"your guess is {guess} and program's guess is {number}")
else:
    print("exiting")
    exit
 
 

output:
Welcome to the guessnum
your guess is 2 and the program's guess is 5

So, we can see that we are almost halfway, we have the guess of the program and the guess of the user. Now we can just compare and print if the user is correct or not.

# Intro Panel Command line
import random
print("Welcome to the guessnum")
 
name=input("what is your name?")
age=int(input(f"Hello {name}, what is your age?"))
choice=input(f"Hello {name}, would you like to play the game? y/n")
 
if choice=="y":
    number=int(random.randint(1,5))
    guess=int(input("Please input your guess"))
 
    if guess==number:
        print("you guessed it right!!!")
 
   
 
    print(f"your guess is {guess} and program's guess is {number}. Sorry!!! your guess is wrong")
 
else:
    print("exiting")
    exit
 
 
output:
Welcome to the guessnum
your guess is 2 and the program's guess is 1. Sorry!!! your guess is wrong

As you can see, I have guessed wrong maybe you can guess it right. This game can be made more interesting by adding the score factor. Now let’s code for the score factor.

# Intro Panel Command line
import random
print("Welcome to the guessnum")
 
name=input("what is your name?")
age=int(input(f"Hello {name}, what is your age?"))
choice=input(f"Hello {name}, would you like to play the game? y/n")
correct=0
 
 
   
 
while(choice=="y"):
    number=int(random.randint(1,5))
    guess=int(input("Please input your guess"))
 
    if guess==number:
        print("you guessed it right!!!")
        correct+=1
        choice=input(f"Hello {name}, would you like to continue the game? y/n")
           
 
   
   
 
    print(f"your guess is {guess} and program's guess is {number}. Sorry!!! your guess is wrong")
    choice=input(f"Hello {name}, would you like to continue the game? y/n")
       
 
 
else:
    print(f"your score is {correct}")
    print("exiting")
    exit
 
 

output:
Welcome to the guessnum
your guess is 1 and program's guess is 5.
Sorry!!! your guess is wrong your guess is 2 and program's guess is 3.
Sorry!!! your guess is wrong your guess is 3 and program's guess is 2.
Sorry!!! your guess is wrong your guess is 4 and program's guess is 3.
Sorry!!! your guess is wrong your guess is 1 and program's guess is 2.
Sorry!!! your guess is wrong your guess is 2 and program's guess is 5.
Sorry!!! your guess is wrong your guess is 3 and program's guess is 4.
Sorry!!! your guess is wrong your guess is 3 and program's guess is 2.
Sorry!!! your guess is wrong your guess is 3 and program's guess is 5.
Sorry!!! your guess is wrong your guess is 4 and program's guess is 2.
Sorry!!! your guess is wrong your guess is 3 and program's guess is 1.
Sorry!!! your guess is wrong your guess is 4 and program's guess is 5.
Sorry!!! your guess is wrong your guess is 2 and program's guess is 2.
you guessed it right!!!
Sorry!!! your guess is wrong your score is 1 exiting

As you can see, we utilized while loops and we used a new variable called correct, which is giving us the score of the user. Which we are printing to the output.

Python_applications

You may also like our JavaScript Course from Beginner to Advanced.

Congratulations! Now you know how to put Python applications in practice, and you officially finished the course: Python Course from Beginner to Advanced in 11 blog posts
Python applications in practice. Part 11 Python Course from Beginner to Advanced in 11 blog posts robert whitney avatar 1background

Author: Robert Whitney

JavaScript expert and instructor who coaches IT departments. His main goal is to up-level team productivity by teaching others how to effectively cooperate while coding.

Python Course From Beginner to Advanced in 11 blog posts:

  1. Introduction to Python Course. Part 1 Python Course from Beginner to Advanced in 11 blog posts
  2. Variables and Data Types in Python. Part 2 Python Course from Beginner to Advanced in 11 blog posts
  3. Python tuples, lists, sets and dictionaries. Part 3 Python Course from Beginner to Advanced in 11 blog posts
  4. Python sets and dictionaries. Part 4 Python Course from Beginner to Advanced in 11 blog posts
  5. Conditional statements in Python. Part 5 Python Course from Beginner to Advanced in 11 blog posts
  6. Loops in Python. Part 6 Python Course from Beginner to Advanced in 11 blog posts
  7. Python functions. Part 7 Python Course from Beginner to Advanced in 11 blog posts
  8. Advanced functions in Python. Part 8 Python Course from Beginner to Advanced in 11 blog posts
  9. Python classes and objects. Part 9 Python Course from Beginner to Advanced in 11 blog posts
  10. Files in Python. Part 10 Python Course from Beginner to Advanced in 11 blog posts
  11. Python applications in practice. Part 11 Python Course from Beginner to Advanced in 11 blog posts