Why can't I get a variable to equal another variable?
Started by Kansas, Aug 01 2014 10:35 PM
11 replies to this topic
#1
Posted 01 August 2014 - 10: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.
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.
#2
Posted 01 August 2014 - 11:24 PM
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 by LeonTheMisfit, 01 August 2014 - 11:25 PM.
#3
Posted 01 August 2014 - 11:34 PM
That is exactly it! Thanks a lot, I'm still new to programming
#5
Posted 02 August 2014 - 04:33 AM
I know this is a bit late, but you could also do y = tonumber(read()), then just do b == y
#6
Posted 02 August 2014 - 06:50 AM
And if you really want to make sure you're getting correct input you could do something like this
tonumber returns nil if the given string isn't a valid number therefore this loop won't exit until a valid number is provided.
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.
#7
Posted 02 August 2014 - 07:02 AM
As has been said from other people,
Instead,
repeat x until yis a bad practice.
Instead,
local y while true do term.write( "Number: " ) y = tonumber( read() ) if y then break end end
#9
Posted 02 August 2014 - 07:17 AM
Uh, no, if a repeat loop is the correct loop to use, use a repeat loop.
#10
Posted 02 August 2014 - 07: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
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.
Alice, on 02 August 2014 - 07:02 AM, said:
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 by LeonTheMisfit, 02 August 2014 - 07:29 PM.
#11
Posted 02 August 2014 - 08:29 PM
No, while true loops aren't always discouraged. Again, use the right tool for the job.
#12
Posted 02 August 2014 - 09:23 PM
Lyqyd, on 02 August 2014 - 08:29 PM, said:
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 by LeonTheMisfit, 02 August 2014 - 09:26 PM.
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users











