ArtofShadows.com see more from the shoots! NSFW

Python – Beginner project ‘guess the number’ upgrade(s).

Try the project upgrades below the original code if you want to learn more!

I was doing some studying and wanted some simple project ideas, one of the first recommendations was this video, I stopped on project ‘2. Guess a number (Computer)’ and liked this simple game. So I coded this and it covers quite a few things like variables, while loops, importing module, random, print plus f formatting, conditionals if elif, making a command etc., lots of very useful things. After I did the little project I wanted this to be a tad better so I came up with some simple upgrades and then some harder ones later.

Disclaimer you should have some basic python skills, you need Python installed on your device, I use visual studio code to quickly run these scripts. Another suggestion if using iOS use the app Pyto it’s an amazing app that will let you code and run python with many modules and example scripts built in, especially on iPad. This is a beginner script.

Video Embed explaining the original project 6:57 – 13:13

My original code which is very similar to original seen in video.

#Guess a number game, small hint
import random
def guess(x):
    random_number = random.randint(1, x)
    guess = 0
    while guess != random_number:
        guess = int(input(f'Guess a numer between 1 and {x}: '))
        if guess < random_number:
            print('Guess again. Too low.')
        elif guess > random_number:
            print('Guess again. Too high.')
    print(f'Congrats, you got the right number {random_number}')

guess(20)

Here is the upgraded project info, I will post mine later.

  1. When script runs have its own max number created to get right to the game, don’t hardcode the max number method. Note guess(20) the 20 is max number in the video and guess() is the command that starts the original script and should start your upgraded script too.
  2. Limit the guesses, like 3-5 or whatever you want.
  3. If no correct answer is given restart the script with new numbers.
  4. Let the player know what guess turn they are on and/or when it’s the final turn.

Bonus advanced optional functions!

  1. Give a more precise tip on the final turn, e.g. ‘Ends with a 5’ or ‘Has two digits’ but don’t make it too easy.
  2. Add another command like one to stop the game.
  3. Keep track of wins losses for the session, oh and maybe a command to show ‘Stats’ too.
  4. Your own ideas!

Feel free to post your code in the comments or send me a ‘Kontakt’ with it.

Here is most of the first upgrades that I tried myself below, this uses a bunch of nested conditionals. This can be improved as is much more, like the hardcoded limit should be a variable {limit} etc.

# Updated code adds turn number, last guess, smaller responses,
# starts with its own random max number, last turn warning, 
# ends at 5 turns if nothing correct.

import random
def guess(x):
    random_number = random.randint(1, x)
    print('- You have 5 chances!')
    guess = 0
    turn = 0
    while guess != random_number:
        guess = int(input(f'Number between 1 and {x}? '))
        turn += 1
        if turn != 5:
            if turn == 4:
                print('- Next turn is your last!')
            if guess < random_number:
                print(f'{turn}. {guess} is too low.')
            elif guess > random_number:
                print(f'{turn}. {guess} is too high.')
        else:
            print(f'- None of your answers matched ({random_number}), GAME OVER!')
            exit()
    print(f'{turn} Congrats, you got the right number {random_number}')

guess(random.randint(1,1000))

To try this code above go here, click the ‘Run’ button and enter your answers below. Note this is pretty hard to win with only 5 chances.

2nd Update below, shorter code, maybe a helpful tip will be added to the ‘Next turn is your last’ message on next update. print() in python is really awesome.

# Second update, shortened the code using ternary, grouping variables, limit is smarter.

import random
def guess(x):
    random_number = random.randint(1, x)
    guess, turn, limit = 0, 0, 5 # <---- last number is the limit
    print(f'- You have {limit} chances!')
    while guess != random_number:
        guess = int(input(f'Number between 1 and {x}? '))
        turn += 1
        if turn != limit:
            if turn == limit - 1:
                print('- Next turn is your last!')
            print(f'{turn}. {guess} is too', 'low' if guess < random_number else 'high')
        else:
            print(f'{turn}. None of your answers matched ({random_number}), GAME OVER!')
            exit()
    print(f'{turn} Congrats, you got the right number {random_number}')

guess(random.randint(1,1000))

Leave a Comment