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

Calculator

Started by Groxir, 12 December 2012 - 01:34 AM
Groxir #1
Posted 12 December 2012 - 02:34 AM
I tried to make a calculator but i ran into some errors that i can't figure out myself :/

I get this error: bios:338: [string "Calc"]:13: 'then' expected

The code goes like this:
local x
print ("Please enter a value: ")
x = read()

local z
print ("Please enter a math method: ")
z = read()

local y
print ("Please enter what to "..z.." with "..x..": ")

if z = + then
print (the answer is: "..x+y)
elseif z = - then
prin.. and so on.

Got any help for me? :3
Orwell #2
Posted 12 December 2012 - 02:36 AM
You need quotes around the + to indicate that it's a string, like this:

if z = "+" then
Groxir #3
Posted 12 December 2012 - 02:39 AM
Oh ok, thanks! :D/>
Groxir #4
Posted 12 December 2012 - 02:41 AM
It didn't work :/
Kingdaro #5
Posted 12 December 2012 - 02:52 AM
This line:
print (the answer is: "..x+y)
should go like
print ("the answer is: ".. (x+y))
Orwell #6
Posted 12 December 2012 - 02:55 AM
No one here can tell you what is wrong when the info you give is "it didn't work". Anyway, here are some of your errors:

You see that you need to do the same for this line, right?

elseif z = - then
becomes

elseif z = "-" then

Also, you're taking the sum of string and nil (x+y), that's impossible. Your line (note the missing "):

print (the answer is: "..x+y)

Easy way for now is to directly convert the input to numbers, like this:

local x
print ("Please enter a value: ")
x = tonumber( read() )
And above that, y never gets a value assigned!