My First Python Game – Guess the Number

Sometimes the best way to get started with a new programming language is to do some reverse engineering. This consists of looking at an existing piece of code to find out how it works.

So let’s look at the piece of code for a fairly basic yet fully working Python game of “Guess the Number”. Look at the code. Look at the syntax. Try to understand what this code do, line by line. Tweak the code to see if you can adapt this game.

Here is the code.

To see this code working use the following button. You will even be able to alter the code!

btnTryCodeOnline_Over

Your Challenge:

Could you change this code to count how many attempts you had at guessing the right number. Once you guess the correct number, the program should tell you how many guesses you used.

Share Button
Posted in Computer Science, Python - Intermediate, Python Challenges
3 comments on “My First Python Game – Guess the Number
  1. Great game!

     

    Here’s a suggestion: make the input prompt change based on whether the guess was too high or low.

     

    Here’s a link to a revised Trinket:

    https://trinket.io/python/d7ed39f852

     

    And here’s the code:

     

    • 101CC says:

      Hi Elliott,

      Thanks, that’s a good idea indeed. Thanks for sharing the code too! I agree with you it’s neater to customise the input message this way.

  2. Here’s the Trinket embedded, to see if it will work:

    (I went to Tools > Source code and pasted in the Embed code)

Our Latest Book

Follow this blog