Difference between revisions of "Calculator Tutorial"

From ComputerCraft Wiki
Jump to: navigation, search
(Created page with "add = "add" subtract = "subtract" multiply = "multiply" devide = "devide" print ("Do you want to add, subtract, multiply or devide?") write ("") put = io.read() write ("Enter ...")
 
m (Moved to tutorials category)
 
(6 intermediate revisions by 5 users not shown)
Line 1: Line 1:
add = "add"
+
<code>
subtract = "subtract"
+
 
multiply = "multiply"
+
-- anything after two dashes is a comment, and wont be compiled.
devide = "devide"
+
--[[ this is also a comment
print ("Do you want to add, subtract, multiply or devide?")
+
and so is this
write ("")
+
]]--
put = io.read()
+
write ("Enter your first number: ")
+
--this tutorial will teach you the basics of the print() and read() functions, if statements, as well as some variable use and math operations.
input = io.read()
+
write ("Enter your second number: ")
+
output = io.read()
+
print("Do you want to add, subtract, multiply or divide?") -- this code displays the question within the quotation marks
if put == add then
+
op = read() -- this code creates a variable called 'op', the read() function stalls the program to accept user input, which is stored in op
x = input + output
+
print("") -- just gives us some space to work with
print ("That = ", x)
+
elseif put == subtract then
+
print("what is the first number to be operated on?")
x = input - output
+
print ("That = ", x)
+
num1 = tonumber(read()) -- accepts the first number to be stored in the variable 'number1'
elseif put == multiply then
+
print("")
x = input * output
+
print ("That = ", x)
+
print("and the second number?")
elseif put == devide then
+
num2 = tonumber(read())
x = input / output
+
print("")
print ("That = ", x)
+
end
+
-- now we operate on the number depending on what the user typed first
sleep(2)
+
term.clear()
+
if op == "add" then -- if you need help with conditional statements, see this page: [http://www.troubleshooters.com/codecorn/lua/luaif.htm]
term.setCursorPos(1, 1)
+
  result = num1+num2
 +
print(result) -- this prints the result
 +
end
 +
 +
if op == "multiply" then
 +
  result = num1*num2 -- asterisk represents multiply
 +
print(result)
 +
end
 +
 +
if op == "divide" then
 +
  result = num1/num2
 +
print(result)
 +
end
 +
 +
if op == "subtract" then
 +
  result = num1-num2
 +
print(result)
 +
end
 +
 
 +
</code>
 +
[[Category:Tutorials]]

Latest revision as of 07:54, 26 March 2014

-- anything after two dashes is a comment, and wont be compiled.
--[[ this is also a comment
and so is this
]]--

--this tutorial will teach you the basics of the print() and read() functions, if statements, as well as some variable use and math operations.


print("Do you want to add, subtract, multiply or divide?") -- this code displays the question within the quotation marks
op = read() -- this code creates a variable called 'op', the read() function stalls the program to accept user input, which is stored in op
print("") -- just gives us some space to work with

print("what is the first number to be operated on?")

num1 = tonumber(read()) -- accepts the first number to be stored in the variable 'number1'
print("")

print("and the second number?")
num2 = tonumber(read())
print("")

-- now we operate on the number depending on what the user typed first

if op == "add" then -- if you need help with conditional statements, see this page: [1]
 result = num1+num2
print(result) -- this prints the result
end

if op == "multiply" then
 result = num1*num2 -- asterisk represents multiply
print(result)
end

if op == "divide" then
 result = num1/num2
print(result)
end

if op == "subtract" then
 result = num1-num2
print(result)
end