🚧Summaries For Lessons 24+ Are Still Work In Progress!

“Whoever is patient has great understanding…" Proverbs 14:29

Exercise: Guessing Number Game

Description here...

Exercise: Guessing Number Game

Description here...

Exercise: Guessing Number Game

Description here...

- Practice Logical Statements
- Learn New Python Concept
- Create Custom Guessing Game
- Have Fun🎉!

H2

Ready to make another Python Game?

This time we're going to create a Guessing Game🤔.
It's a fun one and not so complicated, perfect for us to practice.

💡 The idea is:

🔹We'll create a secret number
🔹Ask user to guess it
🔹Then check if it's Higher/Lower and let user know
🔹Or Congratulate🎉 on guessing it right!

Same as before, stop scrolling or watching the video and try to do it on your own.

You can use Google but avoid using ChatGPT. It'll steal your opportunity to learn and grow your python skill. So try it first and then come back, and I'll also introduce you briefly to the next concept you'll learn in Python.


Ready? Set. Code!

🧠 Brainstorm

So, let's begin with a quick brainstorming session to write out all the main steps. In this case it's very simple and straight-forward, but I want you to get into a habit of creating these Code-Skeletons every time you begin to code.

# Rules

# Ask User for Input

# Check Results
# Rules

# Ask User for Input

# Check Results
# Rules

# Ask User for Input

# Check Results

Now it's time to code each step one by one

🤫Secret Number

While we're in the development stage, it's best to just hardcode a random value and continue. Later we'll come back and actually make it random so we won't know the answer.

It's very common to code quick and dirty to get to the end and then fix all your steps. That's the whole point of creating MVP - Minimum Viable Product.

So just write

secret_num = 7
secret_num = 7
secret_num = 7

🙋‍♂️Ask User To Guess And Check It

Next, we need to get user input().
For now keep it simple.

secret_num = 7
guess      = input('❓Guess the secret number between 1 and 10: ')

if guess == secret_num:
    print("You've guessed it🎉!"
secret_num = 7
guess      = input('❓Guess the secret number between 1 and 10: ')

if guess == secret_num:
    print("You've guessed it🎉!"
secret_num = 7
guess      = input('❓Guess the secret number between 1 and 10: ')

if guess == secret_num:
    print("You've guessed it🎉!"

Now we can test it. And you'll notice that it doesn't work... How is it possible that 7 != 7?

That's very common when you just start.

Logically you compare 2 numbers. But in reality you're comparing a integer to a string. Therefore you get False .

💡 Remember that input() will return you a string of whatever user wrote. That's why it's important to remember types your variables.

Let's fix that by adding int() to our guess variable"

# Rules
secret_num = 7

# User Input
guess      = input('❓Guess the secret number between 1 and 10: ')
guess      = int(guess)                             # Convert String into Integer!

# Check Results
if guess == secret_num:
    print("You've guessed it🎉!"
# Rules
secret_num = 7

# User Input
guess      = input('❓Guess the secret number between 1 and 10: ')
guess      = int(guess)                             # Convert String into Integer!

# Check Results
if guess == secret_num:
    print("You've guessed it🎉!"
# Rules
secret_num = 7

# User Input
guess      = input('❓Guess the secret number between 1 and 10: ')
guess      = int(guess)                             # Convert String into Integer!

# Check Results
if guess == secret_num:
    print("You've guessed it🎉!"

Alright, now our math is working. Back to science!

Sign-Up For Future Updates✨

Be among the first people to hear about
New Python Courses or Useful Resources!

Once there's enough demand I might start a Python Newsletter
with even more Tips and Tricks to help you learn it better!


Want To Donate? Click here.

Sign-Up For Future Updates✨

Be among the first people to hear about
New Python Courses or Useful Resources!

Once there's enough demand I might start a Python Newsletter
with even more Tips and Tricks to help you learn it better!


Want To Donate? Click here.

Sign-Up For Future Updates✨

Be among the first people to hear about
New Python Courses or Useful Resources!

Once there's enough demand I might start a Python Newsletter
with even more Tips and Tricks to help you learn it better!


Want To Donate? Click here.

🧠Add More Logic

Next, we need to address what happens if user doesn't guess it right or even provides numbers outside of our min/max range.

So we need more elif statements. And let's also replace hard coded min/max values with variables so we can reuse these values in multiple places without getting confused or breaking our code.

P.S. There are built-in functions min/max so avoid using these exact names for variables. Instead add an underscore _ in the end.

So let's implement all of that:

#🎯 Rules
min_       = 1
max_       = 10
secret_num = 7 #🤐Set any number you like!

#🙋‍♂️ Ask User for Input!
guess = input(f'❓Guess the secret number between {min_} and {max_}: ')
guess = int(guess)

#💡 Check the Input
if guess < min_ or guess > max_:
    print(f'⛔Incorrect Input. Guess a number between {min_} and {max_}')
 
elif guess == secret_num:
    print('🥳Correct! You guessed it!')
    break

elif guess > secret_num:
    print('❌ Too High! Try Again.')

else:
    print('❌ Too Low! Try Again.')

#🎯 Rules
min_       = 1
max_       = 10
secret_num = 7 #🤐Set any number you like!

#🙋‍♂️ Ask User for Input!
guess = input(f'❓Guess the secret number between {min_} and {max_}: ')
guess = int(guess)

#💡 Check the Input
if guess < min_ or guess > max_:
    print(f'⛔Incorrect Input. Guess a number between {min_} and {max_}')
 
elif guess == secret_num:
    print('🥳Correct! You guessed it!')
    break

elif guess > secret_num:
    print('❌ Too High! Try Again.')

else:
    print('❌ Too Low! Try Again.')

#🎯 Rules
min_       = 1
max_       = 10
secret_num = 7 #🤐Set any number you like!

#🙋‍♂️ Ask User for Input!
guess = input(f'❓Guess the secret number between {min_} and {max_}: ')
guess = int(guess)

#💡 Check the Input
if guess < min_ or guess > max_:
    print(f'⛔Incorrect Input. Guess a number between {min_} and {max_}')
 
elif guess == secret_num:
    print('🥳Correct! You guessed it!')
    break

elif guess > secret_num:
    print('❌ Too High! Try Again.')

else:
    print('❌ Too Low! Try Again.')

And now you can play your game! Try it a few times and it's pretty nice, however there is a major issue - you already know the answer, so it's cheating.

We need to fix that ASAP!

🔮Create Random Number

So, let's make sure that we create a random number every time we play. This way we won't know the number and it will actually become a Guessing Game.

For that we need to use randint() (random integer) function from random module.

import random                               # Load the random module in your script
secret_num = random.randint(min_, max_)     # Get random number between min/max values
import random                               # Load the random module in your script
secret_num = random.randint(min_, max_)     # Get random number between min/max values
import random                               # Load the random module in your script
secret_num = random.randint(min_, max_)     # Get random number between min/max values

And now we can try to play.

Now it's more fun because you don't know the answer. But there is a major issue!
We get a new number every time we re-run the script... So it's really hard to win.

Instead, it would be nice if we'd get a few attempts and for that we need to create a Loop.

✨New Concept - 'For-Loop' in Python

We're going a little ahead of ourselves, because that's the topic of the next lesson, but this is the perfect place to demonstrate why we need loops.

In a nutshell, Loops allow you to create a code-block and iterate over it multiple times. Just follow along and you'll learn more about loops in the next lesson.

For example if you execute this piece of code you'll execute the code block with print statement 5 times.

for i in range(5):
    print('-'*50, f'Attempt {i+1}/5')
for i in range(5):
    print('-'*50, f'Attempt {i+1}/5')
for i in range(5):
    print('-'*50, f'Attempt {i+1}/5')

range() - This functions creates a sequence of 5 numbers [0,1,2,3,4]

for i - This initiates the For-Loop and i is a variable for each number from a sequence. So it will be 0 on first iteration, 1 on the second and so on... That's a loop.


Now let's combine it with our Guessing Game so we get at least 5 tries before script execution ends.

💻Final Code:

# 🤔Guess a Number #️⃣ !
import random

# Rules
min_       = 1
max_       = 20
secret_num = random.randint(min_, max_) #🤐Set any number you like!

for i in range(5):
    print('-'*50, f'Attempt {i+1}/5')

    # Ask User for Input!
    guess = input(f'❓Guess the secret number between {min_} and {max_}: ')
    guess = int(guess)

    #💡 Check the Input
    if guess < min_ or guess > max_:
        print(f'⛔Incorrect Input. Guess a number between {min_} and {max_}')

    #👀 Check Results
    elif guess == secret_num:
        print('🥳Correct! You guessed it!')
        break

    elif guess > secret_num:
        print('❌ Too High! Try Again.')

    else:
        print('❌ Too Low! Try Again.')
# 🤔Guess a Number #️⃣ !
import random

# Rules
min_       = 1
max_       = 20
secret_num = random.randint(min_, max_) #🤐Set any number you like!

for i in range(5):
    print('-'*50, f'Attempt {i+1}/5')

    # Ask User for Input!
    guess = input(f'❓Guess the secret number between {min_} and {max_}: ')
    guess = int(guess)

    #💡 Check the Input
    if guess < min_ or guess > max_:
        print(f'⛔Incorrect Input. Guess a number between {min_} and {max_}')

    #👀 Check Results
    elif guess == secret_num:
        print('🥳Correct! You guessed it!')
        break

    elif guess > secret_num:
        print('❌ Too High! Try Again.')

    else:
        print('❌ Too Low! Try Again.')
# 🤔Guess a Number #️⃣ !
import random

# Rules
min_       = 1
max_       = 20
secret_num = random.randint(min_, max_) #🤐Set any number you like!

for i in range(5):
    print('-'*50, f'Attempt {i+1}/5')

    # Ask User for Input!
    guess = input(f'❓Guess the secret number between {min_} and {max_}: ')
    guess = int(guess)

    #💡 Check the Input
    if guess < min_ or guess > max_:
        print(f'⛔Incorrect Input. Guess a number between {min_} and {max_}')

    #👀 Check Results
    elif guess == secret_num:
        print('🥳Correct! You guessed it!')
        break

    elif guess > secret_num:
        print('❌ Too High! Try Again.')

    else:
        print('❌ Too Low! Try Again.')

Now the best part - enjoy playing your very own game written in Python.

I know it's simple, but you wrote this game yourself!
So it will twice as rewarding playing it.
So give it a blast 💥!

💻Final Code:

How many times it took to Win ❓

I managed to beat the game on my 2nd try. I was lucky.

What about you?

Set max_ = 20 and give it a try. And then share this milestone with the community and let us know how many tries it took to beat the game with random number.

Happy Coding!

🙋‍♂️ See you in the next lesson.
- EF

Happy Coding!

🙋‍♂️ See you in the next lesson.
- EF

Happy Coding!

🙋‍♂️ See you in the next lesson.
- EF

Follow along the lesson and create a Guessing Game.

It's a great exercise in Python to practice logical statements and even learn about new concept - for-loop. And don't worry if loops are a bit confusing now. I'll break them down in the next lesson so you become Looping master in Python.

⌨️ Happy Coding!

⌨️ Happy Coding!

⌨️ Happy Coding!

If you have any questions, leave them in the YouTube Comments.

If you have any questions, leave them in the YouTube Comments.

If you have any questions, leave them in the YouTube Comments.

Have a Question?

Have a Question?

Have a Question?

Sign-Up For Future Updates✨

Be among the first people to hear about
New Python Courses or Useful Resources!

Once there's enough demand I might start a Python Newsletter
with even more Tips and Tricks to help you learn it better!


Want To Donate? Click here.

Sign-Up For Future Updates✨

Be among the first people to hear about
New Python Courses or Useful Resources!

Once there's enough demand I might start a Python Newsletter
with even more Tips and Tricks to help you learn it better!


Want To Donate? Click here.

Sign-Up For Future Updates✨

Be among the first people to hear about
New Python Courses or Useful Resources!

Once there's enough demand I might start a Python Newsletter
with even more Tips and Tricks to help you learn it better!


Want To Donate? Click here.

PS. Python can change your career and how you think about problems.
Be Careful 🙂

PS. Python can change your career and how you think about problems.
Be Careful 🙂

PS. Python can change your career and how you think about problems.
Be Careful 🙂

Sposored by LearnRevitAPI.com