I’ve been trying to get this simple Factorial program that takes a numeral and returns it’s factorial…it keeps telling me (n) is a nil value, what am I doing wrong?
function setup ()
print ("hai")
-- defines a factorial function
function fact(n)
if n == 0 then
return 1
else
return n * fact(n-1)
end
end
print ("enter a number:")
a = io.read("*n") --reads a number
print(fact(a))
end
@D4rk_Code Codea doesn’t use io.read from the keyboard in that manner. Here’s a different approach for input of a value. Enter a value into the “value” area and then press “factorial”.
function setup ()
parameter.text("value",0)
parameter.action("factorial",xx)
end
function xx()
output.clear()
print(value.." factorial is ",fact(value))
end
function fact(n)
n=tonumber(n)
if n==0 then
return 1
else
return(n*fact(n-1))
end
end
Thanks, I’m working through book from lua.org “Programming in Lua” to grasp a better understanding of the language, this was one of it’s tutorials, I was wondering why it never worked on my iPad
While the Lua book gives you a good understanding of how to program in Lua, Codea doesn’t do everything in the book. You might also want to go thru the built in documentation of what Codea does so you have an idea of what you can and can’t do. If there’s something you don’t understand, there are plenty of people here that will help.
I am an absolute beginner on coding and I too am following the book but I don’t understand how I am going to learn when It comes to this stuff. Is there a book or something that I can start by that will teach me everything that I need to know when it comes to Codea as well? I don’t get why I would have to learn Codea and then learn Lua as well. Is this one of the only differences? Is there a source that I can use to get all of the differences in the book to the program?
Ignatz I recently started reading one of you’re books. I love it so far! But the loops are not working and it is frustrating me lol. Thank you for your response.
function setup()
for a=1,10 do -- loop to do something 10 times
print(a,a*a) -- prints a number and that number times itself
end
t={2,4,6,8,10,12,14}
for nbr,value in pairs(t) do -- loop to print values in a table.
print(nbr,value) -- print a table position and the value there.
end
end