Variables and Numbers


OK, Mr. CodePress, we’ve learned about letters, printing, and lists. Now, we get to the juicy part: variables. No, these are not like your variables in your math class; these variables are assigned numbers either randomly by the computer, using an algorithm by the computer, or entered using the input tag.

Using “Input”

Input is used to assign a variable to a number, letter, or word. They work with if-then tags (covered in the next post) to make great choice-designed programs (which are pretty much almost every useful program)

Test It Out:

1 print “What number”

2 input g

3 print ,g,

So, like in #3 in the program, to print the input variable, you must put commas around  the variables and close any quotations, like this example below:

print “That’s the”,g,”number!”

The Algorithms

Now, what fun is the program above? Computers can create algorithms to make great calculators! This is a simple +1 calculator below:

1 print “This is the +1 calculator!”

2 print “Type a number”

3 input a

4 a = a + 1

5 print “Your answer is:”,a

The = sign in #4 is actually a “becomes sign!” For example, in this program below:

g=1+2

Whatever g was, g is now equal to 1+2, aka 3.

In #4 in the above program, a, whatever you type in, becomes a+1, thus if you type in 7 after MASICA says “Type a number”, MASICA will say “Your answer is: 8.” Go ahead and try it! Cool, isn’t it?

Random Variables

If you want the computer to randomly assign a variable, type in:

rnd(<to what number>)

,filling in the <to what number> to what number is one above the maximum you want to use in the random pick. For example, if I want to randomly choose a number that is from 1 to 10, I would put in:

rnd(11)

Notice that I didn’t put:

rnd(10)

As then the computer would pick out the number that is from 1-9, not 1-10.

As for grouping them all together, we need the if-then statement stated in the next post!

Happy Coding (and don’t use an Intel Pentium Single-Core)!

Experiencedsitemaker

P.S.: Remember to use the “run” command

Advertisements

The Debate Form

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s