Difference between revisions of "Calculator Tutorial"

From ComputerCraft Wiki
Jump to: navigation, search
(tutorial on several basic functions)
m (Moved to tutorials category)
 
(3 intermediate revisions by 3 users not shown)
Line 15: Line 15:
 
  print("what is the first number to be operated on?")
 
  print("what is the first number to be operated on?")
 
   
 
   
  num1 = read() -- accepts the first number to be stored in the variable 'number1'
+
  num1 = tonumber(read()) -- accepts the first number to be stored in the variable 'number1'
 
  print("")
 
  print("")
 
   
 
   
 
  print("and the second number?")
 
  print("and the second number?")
  num2 = read()
+
  num2 = tonumber(read())
 
  print("")
 
  print("")
 
   
 
   
Line 26: Line 26:
 
  if op == "add" then -- if you need help with conditional statements, see this page: [http://www.troubleshooters.com/codecorn/lua/luaif.htm]
 
  if op == "add" then -- if you need help with conditional statements, see this page: [http://www.troubleshooters.com/codecorn/lua/luaif.htm]
 
   result = num1+num2
 
   result = num1+num2
 +
print(result) -- this prints the result
 
  end
 
  end
 
   
 
   
 
  if op == "multiply" then
 
  if op == "multiply" then
 
   result = num1*num2 -- asterisk represents multiply
 
   result = num1*num2 -- asterisk represents multiply
 +
print(result)
 
  end
 
  end
 
   
 
   
 
  if op == "divide" then
 
  if op == "divide" then
 
   result = num1/num2
 
   result = num1/num2
 +
print(result)
 
  end
 
  end
 
   
 
   
 
  if op == "subtract" then
 
  if op == "subtract" then
 
   result = num1-num2
 
   result = num1-num2
 +
print(result)
 
  end
 
  end
  
 
</code>
 
</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