
Variables: More examples (Python programming)

Upgrade for more content
True or false. When the user has to enter a value, the Python instruction "print" is used.
Pseudocode shows what we want a program to do. Now we will translate pseudocode into the programming language Python. This is what the pseudocode looks like: First we want to note name and age for two users. Then we want to calculate the age difference and output the result. Here's what we do: First we translate the row “Note name one” We do this by creating - declaring - a variable.
We give the variable the name “n1” To assign a value to this variable, we write an equals sign. It’s the user who will input the value, and therefore the Python-instruction input is used, with two parentheses. We are asking the user for “first person’s name”. The question is a text to be printed out on the screen. We show that by putting quotation marks around the text.
Now to the next row in the pseudocode. "Note age one" We note the age of the first person by creating a new variable "age1" With an equals sign for assigning a value to the variable. Like before, it is the user who inputs the value. Therefore, we use the Python-instruction input again. But, we have to do one more thing. These values - ages - must be integers, not text, because we want to calculate the age difference.
So we use the instruction int, and then parentheses. Then we put the input-instruction inside the int-instruction’s parentheses. Now the program knows that what the user puts in should be integers. We ask for the first person's age by inputting this text and putting quotation marks around it. To translate the pseudocode "Note name two" and "Note age two" we write the same as before and note the second person's name as variable n2 and the second person's age as variable age2.
The next row in the pseudocode should calculate age one minus age two and save the result. We create the variable "result" to note the answer. Assign a value to the variable result by using an equals sign. The value is the result of what is noted in the variable age1 minus what is noted in the variable age2. Finally, the program will print the results on the screen.
We use the Python-instruction: print and then two parentheses. In these we simply write what is in the pseudocode: "Name one is result years older than name two" The values for the variables name one, result, and name two are in "n1", "result" and "n2". The Python-instruction print wants only text within its parentheses. The variable result is an integer. Therefore, we need to change it into text.
Then we use the instruction "str". We put the variable result within the parentheses belonging to the instruction "str". Result is still an integer, but the whole instruction "str result" is a text. And now it can be output on the screen. Using plus signs, we show that we want variables and words to go together.
When we input words to be written on the screen, we need to put quotation marks around them. And then we have to insert spaces between words and quotation marks. Otherwise, the computer outputs everything into a single long word. Now let’s test the code. The program asks the user to put in the name and age of the first person.
Lina 16 Then the program asks for the name and age of the second person. Maria 15 Then the computer will output: "Lina is one years older than Maria". And that's right! Lina is 16 years old, and Maria is 15. It worked.
Now try asking different questions, and calculating new things.