This time I’m going to show a program that chooses a random number from 0 to 100 and then asks the user to guess this number. To make it easier to guess, it gives clues by stating if the number entered is higher or lower than the one chosen. Also, when finally guessed, it says how many tries it took the user to do so.
In order for me to learn how to do this, I checked the following page: http://www.pythonforbeginners.com/random/how-to-use-the-random-module-in-python
At first I had a small problem because I wanted to use ‘try’ as a variable name, but I forgot in python that one is a keyword, which are specific words already used in order to tell the program what to do. So naturally, when I tried to run it my program marked SyntaxError: invalid syntax.
Once I realized the mistake I had made, I changed the variable to ‘number_guess’ and everything was fine! Here is where I got more information about keywords so I wouldn’t make that mistake again: http://www.pythonforbeginners.com/basics/keywords-in-python
After that, this is how I wrote my code:
And it runs like this: