Lesson 5

Input: хондан and хонданиРақам

Input: хондан and хонданиРақам

In this lesson: the program gets data from the user.

The whole lesson on one page

Input: хондан and хонданиРақам

In this lesson: the program gets data from the user.

хондан()

хондан() ("read") reads one line from the "Вуруд" ("input") box:

1тағ ном = хондан();
2чоп("Салом, " + ном + "!");
Input
Алӣ
  • In the playground, you type the input in the "Вуруд" box: each line is one input.
  • Each call of хондан() takes the next line.

хонданиРақам()

хондан() always gives back a string. To do math, you need хонданиРақам() ("read a number"):

1тағ а = хонданиРақам();
2тағ б = хонданиРақам();
3чоп("Ҷамъ:", а + б);
Input
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.

A question

You can give хондан and хонданиРақам a question. It is printed before the program reads the input:

1тағ синну = хонданиРақам("Шумо чандсолаед? ");
2чоп("Пас аз 5 сол шумо", синну + 5, "сола мешавед.");
Input
13

Do not write a question in the tasks: the check compares only the output.

When the input runs out

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.

Task: Hello, name

Read a name and print Салом, name!.

Input: One line: a name.

Output: Салом, name!

Task: Sum

Read two numbers and print their sum.

Input: Two lines, each with a number.

Output: One number - the sum.

Task: Square

Read a number and print its square (the number multiplied by itself).

Input: One number.

Output: Its square.

Task: Age

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.