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

Re-run parts of code

Started by survivalstevee, 29 June 2015 - 10:10 PM
survivalstevee #1
Posted 30 June 2015 - 12:10 AM
I've have been creating a rednet program and everything is almost done, but one of the things i want it to do is be able to re run parts of code within it, for one example, in the beginning of my code it detects whether or not the modem is open and if it isn't, it asks if the user would like to open it. so there are three outcomes to this.

1. the modem opens and the program moves on.
2. the modem doesn't open and the program ends
3. the program doesn't understand what is typed and it closes.

so i was wandering if it was possible to replace outcome 3 so that it will rerun this section of code so if outcome 3 occurs, the user can still get outcome 1 or 2 without having to re-open the program.

Thank you in advance,

if you have any questions please ask and i will answer them,

~Survivalstevee
Bomb Bloke #2
Posted 30 June 2015 - 12:40 AM
A "while" or "repeat" loop would generally be used here:

local answer

repeat
  print("Do you want to open the modem? (y/n)")
  answer = read():sub(1,1):lower()
until answer == "y" or answer == "n"
survivalstevee #3
Posted 30 June 2015 - 03:17 AM
thank you, i didnt even know about the repeat loop