Input: хондан and хонданиРақам
In this lesson: the program gets data from the user.
Lesson 5
хондан and хонданиРақамхондан and хонданиРақамIn this lesson: the program gets data from the user.
хондан and хонданиРақамIn this lesson: the program gets data from the user.
хондан()хондан() ("read") reads one line from the "Вуруд" ("input") box:
1тағ ном = хондан();2чоп("Салом, " + ном + "!");
Алӣ
хондан() takes the next line.хонданиРақам()хондан() always gives back a string. To do math, you need хонданиРақам() ("read a number"):
1тағ а = хонданиРақам();2тағ б = хонданиРақам();3чоп("Ҷамъ:", а + б);
2 3
If you write хондан() instead of хонданиРақам(), you get 23, not 5: the two strings are joined (remember lesson 4).
If the input is not a number, the program stops with an error: "се рақам нест: хонданиРақам() рақам интизор буд" ("се is not a number"). A decimal number can be typed with a dot or a comma: 2.5 or 2,5.
You can give хондан and хонданиРақам a question. It is printed before the program reads the input:
1тағ синну = хонданиРақам("Шумо чандсолаед? ");2чоп("Пас аз 5 сол шумо", синну + 5, "сола мешавед.");
13
Do not write a question in the tasks: the check compares only the output.
If the program reads more lines than there are in "Вуруд", there will be an error: "Вуруд тамом шуд" ("the input has ended"). Give the program as many lines as it reads.
Read a name and print Салом, name!.
Input: One line: a name.
Output: Салом, name!
Read two numbers and print their sum.
Input: Two lines, each with a number.
Output: One number - the sum.
Read a number and print its square (the number multiplied by itself).
Input: One number.
Output: Its square.
Read the year of birth and the current year, and print how old the person turns this year.
Input: Two lines: the year of birth, then the current year.
Output: One number.