19 posts
Posted 09 December 2012 - 09:22 AM
Okay, I'm really new to lua and i was trying to make a stripmining program for my turtle. I want to be able to ask how many cycles I want to run, then run them. I should be able to do that with this:
io.write("Number of Cycles: ")
cycles = io.read()
i = 1
repeat
– didn't write the program yet so i have print(i)
i = i + 1
until i == cycles
But when I run it, it prints a load of numbers until it crashes. I think that its the the variable type but i don't know how to do that.
Can someone help me make this work?
Thanks in advanced!
8543 posts
Posted 09 December 2012 - 09:23 AM
Moved to Ask a Pro.
27 posts
Posted 09 December 2012 - 02:22 PM
If you put one into the input, it's not going to work anyway, because you increase i before you check whether or not it's equal to cycles. Something like this would be more appropriate:
write("Number of cycles: ")
local cycles = io.read()
local i = 1
while i ~= cycles
print(i)
i = i + 1
end
I can't really identify the problem, but here's a couple of tips:
- Always make your variables local unless you want other programs to be able to use them. If you use global variables all the time, you will overload your stack and crash your program.
- Use read() and write() instead of io.read() and io.write(), respectively. The io API is an alternative to most functions that should only be used in special cases.
- Unless in special cases, use while or for instead of repeat until. They are normally used for things like this.
See if this code works, and if it doesn't, send a screenshot of your ComputerCraft coding screen and the output screen.
19 posts
Posted 09 December 2012 - 02:57 PM
Okay i input(ed?) the code you have and i got the first picture. Then i added a "do" before print and it worked… until i put in my number of cycles (which was just 5) and i got the second picture. Please bear /w i am really knew, like started 2 days ago new. 1st
http://tinypic.com/r/29fqo87/6 2nd
http://tinypic.com/r/akvamu/6 and this is just the code as you had it (+ the "do" before the print)
http://tinypic.com/r/miccv7/6Ty
27 posts
Posted 09 December 2012 - 03:25 PM
I apologize for the missing do, I'm used to C++ and it's different. Byt you don't need a do before print, you need it after while. So:
while i ~= cycles do
print(i)
end
[Hint: When accidentally triple posting, the correct response isn't to add another post to the mess. -L]
19 posts
Posted 09 December 2012 - 04:10 PM
Uhg, it works correctly until I add the number of cycles I want then it just prints all that random crap and says that it i printed too much or something. What I THINK is happening (just speculating) is that my write is not saving cycles as a number and so adding 1 to i will never equal the number of cycles because it isn't a number. I don't know enough to fix that :/
218 posts
Location
tmpim
Posted 09 December 2012 - 05:51 PM
Ok, i don't know what your problem is, but this is the code I would have writen, it is alot more efficient:
write("Number of Cycles: ")
local cyc = read()
cyc = tonumber(cyc)
for i=1,cyc do
–stripmine code
print(i)
end
This was my output:
http://tinypic.com/r/x4jaxu/6
218 posts
Location
tmpim
Posted 09 December 2012 - 05:54 PM
EDIT TO MY LAST POST:
The problem is yes, as your post above says, cycles is being saved as a string, so tonumber() converts it from a string to a number!
19 posts
Posted 09 December 2012 - 06:19 PM
Thank you so much incinerate and memoryleak21! I kina just guessed on my problem and turns out I was right! The actual mining code should take like 1 minute just move dig dig up move and repeat.
27 posts
Posted 09 December 2012 - 07:28 PM
You're absolutely right, I completely forgot about that!
[P.S. Thanks to whichever mod did that. My internet was being a dumbag and it wouldn't let me know whether or not I had posted. I only had enough time to add another post rather than edit each one before my internet went down again.]
[P.P.S. Is there some way to delete my post?]
8543 posts
Posted 10 December 2012 - 01:16 AM
You're welcome. You cannot delete your own posts, only edit them. However, please don't edit them to remove the contents after other people have replied to them.