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

Lua help.

Started by MediaPeach, 14 September 2014 - 01:41 PM
MediaPeach #1
Posted 14 September 2014 - 03:41 PM
I need help with this number counting thing.
This I what I have so far:
MAXIMUM = 100
STEP = 1
for the_number = 1, MAXIMUM, STEP do print ("The Number is Now..", the_number)
Lyqyd #2
Posted 14 September 2014 - 06:45 PM
Moved to Ask a Pro.
KingofGamesYami #3
Posted 14 September 2014 - 07:01 PM

MAXIMUM = 100
STEP = 1  
for the_number = 1, MAXIMUM, STEP do --#if you are incrementing by 1, you do not have to specify a third value
   print ("The Number is Now..", the_number)  --#print does not accept multiple inputs, I assume you wanted to concat the variable and string
--#the for loop was not closed
edit: I stand corrected about print. It must be term.write that accepts only one parameter…


local maximum = 100 --#I like to follow lua's default capitalization, it helps keep things strait in my head.
for i = 1, maximum do --#notice I only have two variables
  print( "The Number is Now " .. i ) --# place '..' outside of a string to concat the string and variable, as shown here
  sleep( 1 ) --#i assume you don't want to print out everything at the exact same time, thus the 1 second pause
end --#for statments need an end.  Proper indentation helps with that
edit: fixed the print statement (thanks Dragon)
Edited on 14 September 2014 - 10:51 PM
Lyqyd #4
Posted 14 September 2014 - 07:09 PM
Print accepts multiple values just fine.
crazyadam0 #5
Posted 14 September 2014 - 07:11 PM
I don't really understand your question here but I am assuming you want to print out every number from 1 to 100.
In that case you would want this:

local max = 100
local step = 1
for i = 1, max do
  print("The number is now " .. i)
end
The problem you had was that your string concentration (the .. thing) was inside your quote so it literally printed out the sentence: "The number is now.." You need to put that outside of the quotes or it will just write two dots.

I haven't tested this but it should work.

EDIT: NINJA'D
Edited on 14 September 2014 - 05:11 PM
Dragon53535 #6
Posted 15 September 2014 - 12:15 AM

local maximum = 100 --#I like to follow lua's default capitalization, it helps keep things strait in my head.
for i = 1, maximum do --#notice I only have two variables
  print( "The Number is Now " .. the_number ) --# place '..' outside of a string to concat the string and variable, as shown here
  sleep( 1 ) --#i assume you don't want to print out everything at the exact same time, thus the 1 second pause
end --#for statments need an end.  Proper indentation helps with that

This wouldn't work, it would throw an error (Attempting to concatenate string and nil) since you're trying to print the_number which is nil, you should swap that to i so it should be

print("The Number is Now "..i)
Bomb Bloke #7
Posted 15 September 2014 - 12:52 AM
Or, to put it yet another way, the main thing wrong with the original code is a missing "end" to close the "for" block.