1. To start our activity, click on www.replit.com/languages/python3 to open the Replit program.
SIDE NOTE: You may click the images to amplify.
Replit allows you to program in various programming languages (such as Java, C and others) in a very simple way, without having to install any program on your computer.
When you open Replit, the default language is Python.
2. Later, if you want to learn another programming language, you can always select it here:
3. Now look at the code written in the window on the left: print(‘Hello, world!’). This function, the print function, returns the text written inside the brackets as soon as we tell the program to run. To run the program, click on Run.
As you can see, the phrase “Hello, world!” is written in the window on the right.
4. Python can distinguish numbers from text, just as it can distinguish text related to the programming language from text not related to the programming language.
The text related to the programming language is, for example, functions such as print().
All text not related to Python instructions must be enclosed in double quotes (” “) or single quotes (‘ ‘), as is the case with the text ‘Hello, world’, which we want the program to return.
5. Let’s start our quiz with a greeting. To do this, delete what is inside the print function and type ‘Hello, welcome to this quiz!’. To run the program, click on Run. Watch what happens.
6. Next, you need to create a variable that stores a number that will count the times the person taking the quiz gets the questions right. Create the outcome variable. You can think of variables as boxes where we can store information such as numbers, words and other types of information. You should name them according to what you want to keep there. Variable names must begin with a letter or underscore, and be comprised of letters, numbers or underscores only, containing no special characters (such as @ or Ç) and no spaces. For example: result.
7. Next, create the first question of your quiz. When we ask questions, we expect answers; for the computer to be able to use these answers, it has to store them. To do this, you need to create another variable (for example, answer1) where you can store the answer to the question. You also need to create a new function called input(). Write the following code in the program:
In the box on the right, the computer returns what is inside input() and waits for the user to answer. The answer given will be stored in the variable answer1. To run the program, click on Run. The program waits for an answer to the question. Write an answer in the box on the right. Although they are text, answers to questions do not need quotes, since they are not part of the code (which is always written on the left-hand side).
Notice that, despite your answer, nothing happened. This is because the computer has not been told what to do with the answer given. So, what should the computer do? You have to add a condition.
8. Conditions are instructions that allow the computer to make certain decisions according to the information it receives. The word “if” evaluates whether a certain condition is true or false. The computer decides what to do according to the answer.
To write conditions, we need operators. Comparison operators are mathematical signals that allow us to compare two statements or variables. And these are signals you are familiar with! See the next image:
9. Write the following instruction:
What you have just written could read: If the answer is “python”… note that, for the computer, “python” is different from “Python” and “PYTHON”, although to us it means the same thing. The .lower() is a method which changes all the text stored in an answer to lowercase. In other words, if you answer “PYTHON”, the computer transforms the word “PYTHON” into “python” before checking whether the answer is correct. In this way, you can write the word in uppercase or lowercase and it will always be right… but only if you write “Python”, of course!
10. Tells the computer what to do if the answer is Python, by adding the following lines:
The first line, result += 1, adds 1 to the result variable, previously set to 0. Then, print(“Right”) tells the program to return the message “Right“. The word else stands for “otherwise”. When you write an if condition, you determine what you want to happen when the condition is true. The else allows you to determine what happens when that same condition is false. In this program, else corresponds to all cases where the answer is not Python. This is why, underneath else, we have a print() function that tells the program to return “Wrong”.
NOTE: pay attention not only to the code you write, but where you write it. Indentations (spaces before each line) are important, since they not only make the code easier to read, but also indicate which instructions depend on which instructions. The line following if (line 7) starts a little further to the right, so the execution of this instruction depends on whether the if condition is true. Poorly made indentations are often the reason why a given code malfunctions.
11. Shall we test it? Click Run.
You already have your quiz with a question and an answer. Create more questions and answers, as in the image.
12. To finish the quiz, let’s calculate the percentage of correct answers. To do this, create a new total variable, with the total number of questions in this quiz.
Then add this line to the code:
What you just used is called an f-string. It allows you to call variable values within a print, in this case, the value of the result variable.
13. In the next line, the percentage variable appears. It calculates the percentage of right answers: divides the result by the total number of questions, and multiplies by 100. The split sign corresponds to the symbol /. The multiplication sign corresponds to the symbol *.
To show the value of the calculated percentage to the user, add this line to the code:
Once again, you have used an f-string. It calls variable values into a print. In this case, it will find the value of the variable percentage, which will be shown in the box on the right.
14. To finish, add this line:
The quiz is finished. Test it by clicking Run again.
15. Create other quizzes with everything you have learned, using the Python programming language. You’re well on your way to becoming a great programmer!