2 posts
Posted 01 August 2014 - 11:35 PM
http://pastebin.com/MQY17TNh#In lines 102 - 111, I'd like to get b == y to work but it just won't.
I've tried to debug it through making another script:
http://pastebin.com/8JbmvhUy..but it all comes up negative.
31 posts
Posted 02 August 2014 - 12:24 AM
It looks like your problem is the way you're using variable y. The
read() function returns a string and you want a number. You'll want to use Lua's
tonumber() function to convert the input string to a number before using it in your loops. So you could do the following
for b = 1, tonumber(y) do
--do stuff
end
Edited on 01 August 2014 - 10:25 PM
2 posts
Posted 02 August 2014 - 12:34 AM
That is exactly it! Thanks a lot, I'm still new to programming
31 posts
Posted 02 August 2014 - 12:46 AM
That is exactly it! Thanks a lot, I'm still new to programming
No problem glad I could help! We were all new at some point.
115 posts
Posted 02 August 2014 - 05:33 AM
I know this is a bit late, but you could also do y = tonumber(read()), then just do b == y
31 posts
Posted 02 August 2014 - 07:50 AM
And if you really want to make sure you're getting correct input you could do something like this
local y
repeat
y = tonumber(read())
until y
tonumber returns nil if the given string isn't a valid number therefore this loop won't exit until a valid number is provided.
882 posts
Location
Behind you.
Posted 02 August 2014 - 08:02 AM
As has been said from other people,
repeat x until y
is a bad practice.
Instead,
local y
while true do
term.write( "Number: " )
y = tonumber( read() )
if y then
break
end
end
7508 posts
Location
Australia
Posted 02 August 2014 - 08:13 AM
As has been said from other people,
repeat x until y
is a bad practice.
who said it was bad practise? and why exactly did they say it was bad practice? :huh:/>
8543 posts
Posted 02 August 2014 - 08:17 AM
Uh, no, if a repeat loop is the correct loop to use, use a repeat loop.
31 posts
Posted 02 August 2014 - 08:19 PM
I'm curious to know who said this was a bad practice and what their reasoning was as well. From my understanding of how the repeat until loop works my example is perfectly acceptable, but I'm open to correction if a valid reason is provided. Also, in many of the languages I work with your example
local y
while true do
term.write( "Number: " )
y = tonumber( read() )
if y then
break
end
end
would actually be considered a bad practice because it has hard to follow flow control, and while true loops are always discouraged even if a break is provided.
Edited on 02 August 2014 - 06:29 PM
8543 posts
Posted 02 August 2014 - 09:29 PM
No, while true loops aren't always discouraged. Again, use the right tool for the job.
31 posts
Posted 02 August 2014 - 10:23 PM
No, while true loops aren't always discouraged. Again, use the right tool for the job.
Okay to say they're always discouraged may have been the wrong way to put that. What I meant was that in many cases there is probably a better way, and one should only use it where it's necessary. Like you said, the right tool for the right job, and what I was really trying to get at is that this isn't one of the cases where it's at all the best way or necessary.
Edited on 02 August 2014 - 08:26 PM