7 posts
                
             
            
                Posted 19 September 2015 - 06:26 PM
                So uh, I'm pretty new to ComputerCraft, and I came across a problem. Here's my code:
http://pastebin.com/mRTujrBNSo as you can see, if someone enters a word that isn't stop, the program gets an error message. And I know this is pointless but I'm trying to make programs with complete functionality, for self-teaching purposes. I just can't think of a way to fix it, since I need to make the string into a number for the if statement. Help?
 
                
             
         
        
        
            
            
                
                    
                
                209 posts
                
                    
                        Location
                        Denmark
                    
                
             
            
                Posted 19 September 2015 - 08:34 PM
                if i understand your program right, and what you are trying to do.. you are missing 1line of code..
function squarethings()
  local e, i = " ", 1
  print("Up to what number should I square:")
  e = read()
  if e == "stop" then
	print("Happy to oblige.")
	return
  else
	e = e + 1
	if i < e then
	  repeat
	   local squared = i * i
	   print(i.." x "..i.." = "..squared)
	   sleep(0.3)
	   i = i + 1 - this is the line you missed
	  until i == e
	  squarethings()
	else
	  print("Error, number already squared, or a word has been inputted. Try again.")
	  sleep(0.2)
	  squarethings()
	end
  end
end
squarethings()
 
                
                    Edited on 19 September 2015 - 06:34 PM
                
             
         
        
        
            
            
                
                    
                
                957 posts
                
                    
                        Location
                        Web Development
                    
                
             
            
                Posted 20 September 2015 - 03:32 PM
                In CC's Lua, Recursive functions only work 256 times.
A recursive call is when you call a function inside of itself:
function squarethings()
  --# some code
  squarethings() --# Makes it recursive
end
You can fix this by using a tail call.
Its really easy to do, just 
return the function:
function squarethings()
  --# some code
  return squarethings()
end
Let us know if you need help with something else :)/>
 
                
             
         
        
        
            
            
                
                    
                
                7 posts
                
             
            
                Posted 20 September 2015 - 09:06 PM
                if i understand your program right, and what you are trying to do.. you are missing 1line of code..
function squarethings()
  local e, i = " ", 1
  print("Up to what number should I square:")
  e = read()
  if e == "stop" then
	print("Happy to oblige.")
	return
  else
	e = e + 1
	if i < e then
	  repeat
	   local squared = i * i
	   print(i.." x "..i.." = "..squared)
	   sleep(0.3)
	   i = i + 1 - this is the line you missed
	  until i == e
	  squarethings()
	else
	  print("Error, number already squared, or a word has been inputted. Try again.")
	  sleep(0.2)
	  squarethings()
	end
  end
end
squarethings()
That was just a mistake. I have that line in the program. Could you tell me how to fix the problem with trying to do arithmetic with e, if it's a word like llama or something?
In CC's Lua, Recursive functions only work 256 times.
A recursive call is when you call a function inside of itself:
function squarethings()
  --# some code
  squarethings() --# Makes it recursive
end
You can fix this by using a tail call.
Its really easy to do, just 
return the function:
function squarethings()
  --# some code
  return squarethings()
end
Let us know if you need help with something else :)/>
Uh… I guess… thanks? But could you fix the problem that I asked about? Again, this is my code.
function squarethings()
  local e, i = " ", 1
  print("Up to what number should I square:")
  e = read()
  if e == "stop" then
    print("Happy to oblige.")
    return
  else
    e = e + 1
    if i < e then
      repeat
       local squared = i * i
       print(i.." x "..i.." = "..squared)
       sleep(0.3)
	   i = i + 1
      until i == e
      squarethings()
    else
      print("Error, number already squared, or a word has been inputted. Try again.")
      sleep(0.2)
      squarethings()
    end
  end
end
squarethings()
Help.
 
                
             
         
        
        
            
            
                
                    
                
                1220 posts
                
                    
                        Location
                        Earth orbit
                    
                
             
            
                Posted 20 September 2015 - 09:24 PM
                I would recommend doing this with your first else statement
else
  e = tonumber(e)   --# this converts e to a number; if it's a non-numeric value (such as a word or letter) then it will be nil
  if e and i < e then --# this checks that e isn't nil then does your check for i < e (this replaces your if i < e then statement)
You should also pay close attention to what HPWebcamAble said about recursion, your program will eventually crash out if someone uses it long enough.  Instead of calling squarethings() from within squarethings(), I'd recommend wrapping your initial call to squarethings() in an infinite loop instead and remove your other two calls.  This will eliminate your recursion problem altogether…
function squarethings()
  --# code here, no calls to squarethings()
end
while true do
  squarethings()
end
 
                
                    Edited on 20 September 2015 - 07:49 PM
                
             
         
        
        
            
            
                
                    
                
                7 posts
                
             
            
                Posted 20 September 2015 - 10:30 PM
                I would recommend doing this with your first else statement
else
  e = tonumber(e)   --# this converts e to a number; if it's a non-numeric value (such as a word or letter) then it will be nil
  if e and i < e then --# this checks that e isn't nil then does your check for i < e (this replaces your if i < e then statement)
You should also pay close attention to what HPWebcamAble said about recursion, your program will eventually crash out if someone uses it long enough.  Instead of calling squarethings() from within squarethings(), I'd recommend wrapping your initial call to squarethings() in an infinite loop instead and remove your other two calls.  This will eliminate your recursion problem altogether…
function squarethings()
  --# code here, no calls to squarethings()
end
while true do
  squarethings()
end
Okay… so this is what I'd do?
function squarethings()
  local e, i = " ", 1
  print("Up to what number should I square:")
  e = read()
  if e == "stop" then
    print("Happy to oblige.")
    return
  else
    e = tonumber(e)
    if e and i < e then
      repeat
       local squared = i * i
       print(i.." x "..i.." = "..squared)
       sleep(0.3)
           i = i + 1
      until i == e
    else
      print("Error, number already squared, or a word has been inputted. Try again.")
      sleep(0.2)
    end
  end
end
while true do
  squarethings()
end
Explain the recursion problem to me, and also explain the if e and i < e statement please.
 
                
             
         
        
        
            
            
                
                    
                
                212 posts
                
                    
                        Location
                        Somewhere in this dimension... I think.
                    
                
             
            
                Posted 20 September 2015 - 11:22 PM
                Another way to fix it is to do
if type(e) ~= "number" then
	--# Error code here
end
Dog's works perfectly fine but for future reference type() is useful.
 
                
                    Edited on 20 September 2015 - 09:34 PM
                
             
         
        
        
            
            
                
                    
                
                3057 posts
                
                    
                        Location
                        United States of America
                    
                
             
            
                Posted 20 September 2015 - 11:23 PM
                Just a note: != isn't a thing in Lua.  In lua, you use ~= for "not equal to"
                
             
         
        
        
            
            
                
                    
                
                212 posts
                
                    
                        Location
                        Somewhere in this dimension... I think.
                    
                
             
            
                Posted 20 September 2015 - 11:25 PM
                I sometimes mix it up so when I code I always double check.
                
             
         
        
        
            
            
                
                    
                
                957 posts
                
                    
                        Location
                        Web Development
                    
                
             
            
                Posted 20 September 2015 - 11:32 PM
                
if type(e) ~= number then
And number should be a string
 
                
             
         
        
        
            
            
                
                    
                
                212 posts
                
                    
                        Location
                        Somewhere in this dimension... I think.
                    
                
             
            
                Posted 20 September 2015 - 11:34 PM
                I promise to you I get it right 100% of the time I actually make the code! I swear!
                
             
         
        
        
            
            
                
                    
                
                7 posts
                
             
            
                Posted 02 October 2015 - 03:01 PM
                I want to know why the code Dog gave me will work
                
             
         
        
        
            
            
                
                    
                
                1220 posts
                
                    
                        Location
                        Earth orbit
                    
                
             
            
                Posted 02 October 2015 - 04:37 PM
                Recursion occurs when you call a function from within itself.  When you were calling squarethings() from within squarethings() you were creating recursion.  Basically you were starting the function again before it stopped the last time.  You can only do that 256 times before the program will crash.  I had you call squarethings() from within an infinite loop that was outside of squarethings() - that way squarethings() gets called repeatedly, but not from within squarethings() - therefor there is no recursion.
As for
if e and i < e then
First I converted e to a number using tonumber() (e.g. the string "7" will be converted to the number 7) - if e can't be converted to a number (e.g. e == "fish") then it will be nil.  Next I checked that e was not nil with the first part of my statement 'if e' - that's the equivalent of…
if e == true
If e is nil (or false) it won't evaluate to true and therefor will go to your else statement.  If, on the other hand, e is not nil and is actually a number then Lua will continue with the next check in my if statement 'and i < e' - so long as i is less than e then the statement will continue to your squaring code.  If i is equal to or greater than e then the code will go to the else statement.
so…
if e and i < e then
basically says, "If e is true and i is less than e then continue"
 
                
                    Edited on 02 October 2015 - 02:43 PM
                
             
         
        
        
            
            
                
                    
                
                7 posts
                
             
            
                Posted 02 October 2015 - 05:52 PM
                Recursion occurs when you call a function from within itself.  When you were calling squarethings() from within squarethings() you were creating recursion.  Basically you were starting the function again before it stopped the last time.  You can only do that 256 times before the program will crash.  I had you call squarethings() from within an infinite loop that was outside of squarethings() - that way squarethings() gets called repeatedly, but not from within squarethings() - therefor there is no recursion.
As for
if e and i < e then
First I converted e to a number using tonumber() (e.g. the string "7" will be converted to the number 7) - if e can't be converted to a number (e.g. e == "fish") then it will be nil.  Next I checked that e was not nil with the first part of my statement 'if e' - that's the equivalent of…
if e == true
If e is nil (or false) it won't evaluate to true and therefor will go to your else statement.  If, on the other hand, e is not nil and is actually a number then Lua will continue with the next check in my if statement 'and i < e' - so long as i is less than e then the statement will continue to your squaring code.  If i is equal to or greater than e then the code will go to the else statement.
so…
if e and i < e then
basically says, "If e is true and i is less than e then continue"
Ah… thanks!
 
                
             
         
        
        
            
            
                
                    
                
                7 posts
                
             
            
                Posted 02 October 2015 - 06:45 PM
                Recursion occurs when you call a function from within itself.  When you were calling squarethings() from within squarethings() you were creating recursion.  Basically you were starting the function again before it stopped the last time.  You can only do that 256 times before the program will crash.  I had you call squarethings() from within an infinite loop that was outside of squarethings() - that way squarethings() gets called repeatedly, but not from within squarethings() - therefor there is no recursion.
As for
if e and i < e then
First I converted e to a number using tonumber() (e.g. the string "7" will be converted to the number 7) - if e can't be converted to a number (e.g. e == "fish") then it will be nil.  Next I checked that e was not nil with the first part of my statement 'if e' - that's the equivalent of…
if e == true
If e is nil (or false) it won't evaluate to true and therefor will go to your else statement.  If, on the other hand, e is not nil and is actually a number then Lua will continue with the next check in my if statement 'and i < e' - so long as i is less than e then the statement will continue to your squaring code.  If i is equal to or greater than e then the code will go to the else statement.
so…
if e and i < e then
basically says, "If e is true and i is less than e then continue"
EDIT 3: Getting rid of my old nonsense, anyway… it's broken now. It starts back at the beginning of the number now. Unsure why it does this.
 
                
                    Edited on 02 October 2015 - 04:53 PM
                
             
         
        
        
            
            
                
                    
                
                1220 posts
                
                    
                        Location
                        Earth orbit
                    
                
             
            
                Posted 09 October 2015 - 07:06 PM
                Post your code as it is now so I can see what changes you've made.
                
             
         
        
        
            
            
                
                    
                
                7 posts
                
             
            
                Posted 06 November 2015 - 07:41 PM
                Post your code as it is now so I can see what changes you've made.
The thing I was using to do my ComputerCraft programming has been broken since a little before you asked this, so I'm not dead, I've just been trying to fix the problem. I know you didn't ask about this, but I just felt like I needed to explain why I haven't shown you.
 
                
                    Edited on 06 November 2015 - 06:42 PM
                
             
         
        
        
            
            
                
                    
                
                1220 posts
                
                    
                        Location
                        Earth orbit
                    
                
             
            
                Posted 06 November 2015 - 08:57 PM
                No worries - post what you've got when you can and we'll go from there :)/>