Jump to content




2 variables are equal, but Lua doesn't think so


4 replies to this topic

#1 capnstoobie

  • Members
  • 5 posts

Posted 18 June 2016 - 06:42 AM

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!

#2 Bomb Bloke

    Hobbyist Coder

  • Moderators
  • 7,099 posts
  • LocationTasmania (AU)

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 capnstoobie

  • Members
  • 5 posts

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 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

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.

#5 capnstoobie

  • Members
  • 5 posts

Posted 19 June 2016 - 04:03 AM

Thanks!





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users