In this challenge you’ll learn about data types – what they are, why we need them and how to convert between different data types.
- We want to move our turtle to a user-defined position. So we have to ask the user for the x and y coordinate. Add a command
Turtle.Ask("Hi! Where should I move?\r\nX coordinate:")
and store the result in a new variablexCoordinateText
.\r\n
is a line break – similar to pressing Enter in a text document. - Run the program, enter a number when the turtle asks you for the X coordinate and press Enter to confirm your input.
Note that you can input any text, not just numbers.
"53"
,"-163.4"
or"slightly right"
are all possible inputs that might be stored inxCoordinateText
. So before we can use the users input as number we have to make sure it actually is a number. We do this by parsing the input as number. - Define a new variable
xCoordinate
and storedouble.Parse(xCoordinateText)
in it. The type of valuesxCoordinate
can store is notstring
butdouble
.double
means real number with double precision. Most of the time you don’t really care about the precision and just use double.double.Parse
is a command that checks if a given text is a real number. If it is, it returns the number as result. If not, it crashes the program (until we talk about error handling). At first it seems to be weird thatdouble.Parse
returns the number although we already have it. The thing is that withxCoordinateText
we can’t do any calculations. You can’t add another number to it, you can’t multiply it by another number, you can’t do any arithmetic operation. This is because the variable is defined to be able to store all sorts of text (type of data isstring
).xCoordinate
on the other hand is defined to store real numbers only (data type isdouble
). And that’s why we can later use it to move the turtle. - Repeat the previous steps to read and parse the y coordinate.
- Use
xCoordinate
andyCoordinate
to move the turtle to this position.