granite-docs/docs/taking-input.md
2024-08-16 14:48:06 +01:00

1.5 KiB

Taking input from the user

Granite has a special operation that allows you to ask your user for a value to place into a variable! This is useful for asking your user things, like what is their name!

New operations

  • :?(variable), take input, asks the user for input, if there is a value in (variable) then prompt them with this otherwise present them with a ? prompt

Tutorial

Your program should look something like this at this point:

:>10,finishing_number
:>1,step
:>0,starting_number
:@count
:astarting_number,step,starting_number
:!starting_number
:-count,starting_number,finishing_number

We want to ask the user to input numbers by giving them a prompt!

At the top of your program, initialise a variable called message and add anything you want! Just make sure it's obvious to the user that you are asking them for numbers then print out this variable immediately after!

Now, we want to ask the user for their numbers!

Replace the variable definitions for finishing_number, step and starting_number with these three statements:

:?starting_number
:?finishing_number
:?step

Now execute your program! You should see something like this:

Enter some numbers!
? 

Now if you put in 0, then 10 and then 1 you will see the same output as you would have before!

Extension

Can you make each prompt for the variables output a message for each one like this?:

Enter some numbers!
Starting number? 0
Finishing number? 10
Step?

Continue the guidebook