Numbers and text
In the first chapter Yu printed what we wrote in quotes. Now we will keep values, count with them and ask the person a question.
Values have names
A name remembers a value. The = sign puts the value into the name:
age = 15 say("I am", age, "years old")Open in Studio
I am 15 years old
The name is always on the left of = and the value on the right. Names may be written in
Ukrainian too, and they may hold an apostrophe: ім'я.
Counting
Yu counts with +, -, * (times), / (divide) and %, the remainder after dividing.
sum = 7 + 8 say("Sum:", sum) say("Half:", sum / 2) say("Remainder after dividing by 4:", sum % 4)Open in Studio
Sum: 15 Half: 7.5 Remainder after dividing by 4: 3
Text joins up
The same + means "join" for text. A number can be joined to text as well.
name = "Olya" say("Hi, " + name + "!")Open in Studio
Hi, Olya!
Look at the space inside "Hi, ": without it the words would stick together.
Ask the person
ask shows a question and waits for an answer. In the Studio the question appears in the
terminal, and that is where the answer is typed.
Olya
name = ask("What is your name?") say("Hi, " + name + "!")Open in Studio
Hi, Olya!
Text and numbers are different
ask always gives back text, even when the person typed digits. To count with it, turn it into a
number with number. The command text goes the other way.
12
years = number(ask("How old are you?")) say("Next year you turn", years + 1)Open in Studio
Next year you turn 13
Round and random
round gives the nearest whole number.
say("2.5 rounds to", round(2.5))Open in Studio
2.5 rounds to 3
random gives a different number every time, so there is no output here: run it and see yours.
say("Dice:", random(1, 6))Open in Studio
Try it yourself
- Ask for two numbers and print their sum.
Show the answer
4 5
first = number(ask("First number:")) second = number(ask("Second number:")) say("Sum:", first + second)Open in Studio
Sum: 9
- Put the name of your city into a name and greet it.
Show the answer
city = "Kyiv" say("Hi, " + city + "!")Open in Studio
Hi, Kyiv!