This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
kodiack5477's profile picture

Need Help with Calculator

Started by kodiack5477, 18 July 2015 - 01:21 AM
kodiack5477 #1
Posted 18 July 2015 - 03:21 AM
I need help with the program found here: http://pastebin.com/1zbm4L4p

Hello ComputerCraft forums! My friend and I are new to ComputerCraft, and we made a simple calculator together. We then wanted to add the functionality for it to repeat again and again without calling the script again and again, however it seems that we have reached found a few bugs. Whenever we try to exit the program by typing in "e" or "E" or "End" or "end", the program just continues normally, We also found another bug where typing in a value for the variable "operation" that is not included in the calculation switch allows the program to continue normally, instead of printing the text "Try again" and restarting from the beginning of the loop. Finally, we discovered a huge bug that basically ruined our program, we can't perform any operations at all! Whenever we try, we get an error.

Thank you all for putting up with novice programmers like ourselves, and your feedback on how to fix our program would be much appreciated.

TL;DR - Our code sucks, help please <3
Edited on 18 July 2015 - 01:39 AM
KingofGamesYami #2
Posted 18 July 2015 - 04:26 AM
There's quite a few problems with your script, I'm going to list them by line number (I may not catch all of them)

2. loop == true is redundant. while loop do will work exactly the same.
7. the or operator doesn't work like that.* Proper usage:

if operation == "end" or operation == "e" then
7b. I recommend using string.lower,

if operation:lower() == "end" or operation:lower() == "e" then
8. Not really a problem, per say, but you could use break instead of setting a variable. Pretty much preference, although break exits the loop immediately.
13 &amp; 15. The variables are defined locally to the function, meaning…
On line 20 you're always going to be adding nil and nil
18. Same problem as on line 7
20. Problem stated above, lines 13 &amp; 15
22. Again, same problem as line 7
24. Same thing as line 20/13/15
26. Misuse of "or" yet again
28. Local variables again
30. Misuse of "or"
32. Local variables again


*Basically, when you say "a == b or c", the interpreter does this:
1. Does a equal b?
Yes -> statement is true
No -> is c nil or false?
..Yes -> the statement is false
..No -> the statement is true
Edited on 18 July 2015 - 02:26 AM
HPWebcamAble #3
Posted 18 July 2015 - 06:16 AM
To add to what King pointed out:

You should define functions and variables first, especially if the whole program will use them.
If a small portion of your code will be using a variable, you can define it there.

So an example template:

--# Define variables
local var1 = "hi"
local var2 = "another value"

--# Define functions
local function destroy() --# Also, its good practice to make functions local too
  var1 = nil
  var2 = nil
end

--# Now start your program
while true do
  local input = read()
  if input == var1 or input == var2 then
    print("that was lucky")
  else
    destroy()
  end
end


Obviously yours is much more useful than this example :)/>
kodiack5477 #4
Posted 18 July 2015 - 11:45 PM
Hey everyone, thank you so much for the input! Thanks to you two, I now have a fully functional calculator. Here is the code for it just in case you want to critique it some more:


--Initialize Variables
local operation
local result
local num1
local num2
local loop
--------------------------------------------------------
--Function to clear text
local function clear()
  term.clear()
  term.setCursorPos(1,1)
end
-------------------------------------------------
-- Greeting and instructions
loop = true
while loop do
textutils.slowPrint("Welcome to EnderCalc Version 0.1!")
print("Type End to cancel the program")
print("Would you like to add, subtract, multiply, or divide?")
operation = read()
if operation == "end" or operation == "End" or operation == "e"
or operation == "E" or operation == "END" then
print("Exiting Endercalc...")
sleep(2)
clear()
break
end
--------------------------------------------------
--Function to get numbers
local function nums()
  print("Enter the first number to be operated on please: ")
  num1 = tonumber(read())
print("Enter the second number to be operated on please")
  num2 = tonumber(read())
end
--------------------------------------------------
--Arithmetic operations
if operation == "add" or operation == "ADD" or
operation == "a" or operation == "A" then
  nums()
  result = num1 + num2
  print(result)
  sleep(1.5)
  clear()
elseif operation == "subtract" or operation ==  "Subtract"
or operation == "s" or operation == "S" then
  nums()
  result = num1 - num2
  print(result)
  sleep(1.5)
  clear()
elseif operation == "multiply" or operation == "Multiply"
or operation == "m" or operation == "M" then
  nums()
  result = num1 * num2
  print(result)
  sleep(1.5)
  clear()
elseif operation == "divide" or operation == "Divide"
or operation == "d" or operation == "D" then
  nums()
  result = num1 / num2
  print(result)
  sleep(1.5)
  clear()
else
  print("It seems like you entered an unrecognized value, please try again")
end
end