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

Program not working help.

Started by SPC2000, 15 October 2012 - 02:28 PM
SPC2000 #1
Posted 15 October 2012 - 04:28 PM
Hi.

I made a program in lua, but it dosnt work.

I wanted to make a timer and i get an error when running it.

Please describe whats wrong.

Code:
print("Minutes:")
m = read()
term.clear()
print("Hours:")
h = read()
l = true
while l = true then
term.clear()
print("Hours:")
print(h)
print("")
print("Minutes:")
print(m)
sleep(60)
m = m-1
if m = -1 do
h = h-1
m = 59
end
if h = -1
l = false
end
end
term.clear()
print("Ready")
redstone.Output("back", true)
sleep(120)
redstone.Output("back", false)
os.shutdown()

My english might be a bit bad. Sorry about that.

Any help would be apriciated.
remiX #2
Posted 15 October 2012 - 04:33 PM
Could you give us the error code?

I skimmed through it fast because the indentation is killing my eyes.

while l = true then
must be
while l == true do
or
while l do

if h = -1
and
if m = -1 do
must be
if h == -1 then
and
if m == -1 then

setting a variable you use a single equal sign, if you are comparing two variables such as in your if functions you must use 2 =
sjele #3
Posted 15 October 2012 - 04:36 PM
And allso use
 tags. Thats helps

[code*]
--Your code here:
[*/code]

Remove the *'s
remiX #4
Posted 15 October 2012 - 05:06 PM
Oh and
redstone.Output("back", true)
needs to be
redstone.setOutput("back",true)
-- or
rs.setOutput("back",true)

If i'm correct, this is some sort of timer that you put on and after a specific time it will put the redstone signal on the back of the computer. If so, this code should be fine.


term.clear()
term.setCursorPos(1,1)
write("Minutes: ")
m = read()
write("nHours: ")
h = read()
while true do    -- You don't need to use a variable for this, just while true do :D/>/>'
    term.clear()
    term.setCursorPos(1,1)    -- resets the cursor position to top left, it just seems neater with this.
    print("Hours: "..h)    -- Concatenates the number of hours left into the same string so you do not have to have it on a separate line (unless it want it to)
    print("nMinutes: "..m)
    sleep(60)
    m = m-1
    if h == 0 and m == 0 then    -- If the number of hours is 0 AND minutes is 0, the timer is finished.
        term.clear()
        term.setCursorPos(1,1)
        print("Ready")
        rs.setOutput("back", true)
        sleep(120)
        rs.setOutput("back", false)
        return false
    end
    if m == 0 then    -- Not sure why you had it on -1, because then it shows the minute as 0 when it gets there.
        h = h-1
        m = 60
    end
end
SPC2000 #5
Posted 15 October 2012 - 07:31 PM
Thanks, it helped me out alot :D/>/>

I can finaly finish my stuff in Tekkit!
remiX #6
Posted 15 October 2012 - 08:45 PM
If you want the code to be more professional, like when you enter the hour or minute it has to be a number and doesn't accept strings; decimals; negatives; or numbers over 60 and where the hour isn't negative either - I just put this code together for you.

If there is anything from this code you aren't sure about, feel free to ask :D/>/>


m = nil
h = nil

function clear()
	term.clear()
	term.setCursorPos(1,1)
end

function checkTime()
	while true do
		clear()
		print("Counting Down...n")
		if h > 0 then
			print("Hours: "..h)
			print("Minutes: "..m)
		elseif m == 1 then
			print(m.." minute remaining!")
		elseif m >= 2 or m <= 60 then
			print(m.." minuites remaining!")
		end
		sleep(0.1)
		m = m-1
		if h == 0 and m == 0 then
			clear()
			print("Countdown complete. Activating redstone signal for 2 minutes.")
			rs.setOutput("back", true)
			sleep(120)
			rs.setOutput("back", false)
			m = nil
			h = nil
			return false
		end
		if m == 0 then
			h = h-1
			m = 60
		end
	end
end

function minutes()
	clear()
	write("Minutes: ")
	m = tonumber(read())
	if type(m) ~= 'number' then
		print("Please enter a valid number!")
		m = nil
		sleep(2)
		begin()
	elseif string.find(m, "%.") then
		print("No decimal holders allowed!")
		m = nil
		sleep(2)
		begin()
	elseif m <= -1 or m >= 61 then
		print("Please enter a number between 0 and 60!")
		m = nil
		sleep(2)
		begin()
	elseif m >= 0 or m <= 60 then
		hours()
	end
end

function hours()
	write("Hours: ")
	h = tonumber(read())
	if type(h) ~= 'number' then
		print("Please enter a valid number!")
		h = nil
		sleep(2)
		begin()
	elseif string.find(h, "%.") then
		print("No decimal holders allowed!")
		h = nil
		sleep(2)
		begin()
	elseif h <= -1 then
		print("Please enter a positive number.")
		h = nil
		sleep(2)
		begin()
	end
	checkTime()
end

function begin()
	if m == nil then
		minutes()
	else
		hours()
	end
end

begin()
SPC2000 #7
Posted 16 October 2012 - 09:33 AM
Interesting… The "function" is that not like a alias, Or something else?

I am not good at lua, but might be one day :D/>/>
Sammich Lord #8
Posted 17 October 2012 - 04:17 AM
Interesting… The "function" is that not like a alias, Or something else?

I am not good at lua, but might be one day :D/>/>
A function is basically like a set of code. Kinda like a mini program that you can call over and over again.