As part of this project you will learn:
Computer Programming or Coding is the process of writing instructions for a computer to follow. It's like a recipe, but for a computer. The instructions tell the computer what to do and in what order to do it.
Those sets of instructions that the computer follows to do something are written in a Programming Language.
We will be learning a text-based programming language called Python.
Students may create a free account to save all the projects-> click here, and note that parental permission is required to create accounts for children under 13.
IMPORTANT!: It's important to understand that Trinket creates public web pages and projects that anyone with a link can view. Young people may need a reminder about the importance of not sharing personal information online.
Always place import statements at the top of your code.
To generate a random number between 1 and 100, we will use the randint() function from the random module. Add the following line to your code to generate a random number:
This will assign a random integer between 1 and 100 to the variable secret_number.
To ask the user to guess the secret number, we will use the print() and input() functions. In Python, the input() function is used to get input from the user, and the print() function is used to display output to the user.
The int() function is used to convert a string to an integer. In this project, we use the int() function to convert the user's input (which is initially a string) to an integer so that we can compare it with the secret number.
Add the following lines to your code:
Run your Code. This will display a message to the user asking them to guess the secret number, and will then get their guess as input from the keyboard.
Run your Code. This will keep asking the user to guess the secret number until their guess is correct. If their guess is too low, it will print "Too low!" on the screen. If their guess is too high, it will print "Too high!" on the screen. The guess variable will be updated with their new guess each time through the loop.
Add a print statement to congratulate the user when they guess the number correctly. You can do this by adding the following line of code on the same level with a while loop:
You have completed this project.