Hope someone can help me with this maddening problem.
I am trying to write a program that will make a turtle construct a platform, based on the length and width entered by the user. The turtle is supposed to "snake" back and forth, placing blocks, until the platform is complete.
In the program, an IF statement tests the platform_length variable against a counter, and when they equal it should execute the "first turn". My problem is that Lua never believes those 2 variables to be equal, even though a print statement I put in there for debugging shows that they are!
The program does execute the "second turn", which is a similar expression, so I can't work out why my "first turn" is not working. The code is simple, so I can only think I'm missing a Lua fundamental?
The program is here: http://pastebin.com/D8EtHNpr
thanks in advance if you can help!
0
2 variables are equal, but Lua doesn't think so
Started by capnstoobie, Jun 18 2016 06:42 AM
4 replies to this topic
#1
Posted 18 June 2016 - 06:42 AM
#2
Posted 18 June 2016 - 07:52 AM
print(type(turncounter),type(platform_length))
write("Enter width: ") local platform_width = tonumber(read()) write("Enter length: ") local platform_length = tonumber(read())
#3
Posted 18 June 2016 - 08:49 AM
Fantastic - thanks for your help! What confused me was that "second turn" worked and "first turn" didn't. It's all working now with that simple change - thanks
#4
Posted 18 June 2016 - 01:01 PM
Just to add a little explanation behind what Bomb Bloke showed there.
When you get input from the user it will always be a string, it is up to you to convert it to a number. The problem you had was you were comparing a string to a number, and "2" is never equal to 2 unless they're the same data types.
When you get input from the user it will always be a string, it is up to you to convert it to a number. The problem you had was you were comparing a string to a number, and "2" is never equal to 2 unless they're the same data types.
#5
Posted 19 June 2016 - 04:03 AM
Thanks!
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users