This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
Mr0reo's profile picture

While loop problem

Started by Mr0reo, 21 January 2013 - 11:05 AM
Mr0reo #1
Posted 21 January 2013 - 12:05 PM
I am trying to make a simple code that will take a number from the user and pulse a redstone signal that many times. But when I run it it says "Attempt to compare string with number expected, got string". my question is why does it think that I am inputting a string, and how do I fix it?


print("Enter #")
a = read()
i = 0
  while a>i do
  rs.setBundledOutput("right", colors.green)
  sleep(2)
  print("done")
  rs.setBundledOutput("right", colors.red)
  sleep(0.2)
  i = i+1
  end
  
mibac138 #2
Posted 21 January 2013 - 12:10 PM

print("Enter #")
a = tonumber(read())
i = 0
  while a > i do
  rs.setBundledOutput("right", colors.green)
  sleep(2)
  print("done")
  rs.setBundledOutput("right", colors.red)
  sleep(0.2)
  i = i+1
  end

Maybe this? :)/>
Luanub #3
Posted 21 January 2013 - 12:10 PM
read() outputs a string. do "a = tonumber(a)" after the the read to convert it. For the way you're using the loop a numerical for loop would be a better option. It will do the incrementing for you an eliminate the need for i.


print("Enter #")
a = read()
for i=1, tonumber(a) do
  rs.setBundledOutput("right", colors.green)
  sleep(2)
  print("done")
  rs.setBundledOutput("right", colors.red)
  sleep(0.2)
end
Mr0reo #4
Posted 21 January 2013 - 12:13 PM
Thanks for the help it worked :)/>