Jump to content




While loop problem


  • You cannot reply to this topic
3 replies to this topic

#1 Mr0reo

  • Members
  • 2 posts

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
  


#2 mibac138

  • Members
  • 132 posts
  • LocationPoland

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? :)

#3 Luanub

    Lua Nub

  • Members
  • 1,135 posts
  • LocationPortland OR

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


#4 Mr0reo

  • Members
  • 2 posts

Posted 21 January 2013 - 12:13 PM

Thanks for the help it worked :)





2 user(s) are reading this topic

0 members, 2 guests, 0 anonymous users