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
Output
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
Output
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
Output
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.

You type
Olya
name = ask("What is your name?")
say("Hi, " + name + "!")
Open in Studio
Output
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.

You type
12
years = number(ask("How old are you?"))
say("Next year you turn", years + 1)
Open in Studio
Output
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
Output
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

  1. Ask for two numbers and print their sum.
Show the answer
You type
4
5
first = number(ask("First number:"))
second = number(ask("Second number:"))
say("Sum:", first + second)
Open in Studio
Output
Sum: 9
  1. Put the name of your city into a name and greet it.
Show the answer
city = "Kyiv"
say("Hi, " + city + "!")
Open in Studio
Output
Hi, Kyiv!