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

Loop error?

Started by Expenox, 25 November 2012 - 03:44 PM
Expenox #1
Posted 25 November 2012 - 04:44 PM
Both of these do not work, and I want to know if someone can help me out?
Well title needs to be changed now… But my first one i had the "or" in it and i entered Expenox it didnt work then I entered expenox still didn't work.
Rushed code.

For my "or" attempt at the program door lock the code is:


os.pullEvent = os.pullEventRaw
local password = ""
term.write("Enter Password:")
while password ~= "Expenox" or "expenox"
print("Incorrect Password!")
sleep(2)
print("Enter Password:")
while password = "Expenox" or "expenox"
print("Correct Password!")
set.redstoneOutput("back", true)
sleep(6)
set.redstoneOutput("back", false)
os.reboot()

And for my one without or:


os.pullEvent = os.pullEventRaw
local password = ""
term.write("Enter Password:")
while password ~= "Expenox" do
print("Incorrect Password!")
sleep(2)
print("Enter Password:")
while password = "Expenox" do
print("Correct Password!")
set.redstoneOutput("back", true)
sleep(6)
set.redstoneOutput("back", false)
os.reboot()
Kingdaro #2
Posted 25 November 2012 - 04:53 PM
If you haven't followed any sort of tutorial prior to doing this, you probably should.

If you have, try paying closer attention, and your various errors may become much more obvious.
Expenox #3
Posted 25 November 2012 - 05:02 PM
LOL my bad. Didn't look over my code good enough.

How could i mess that up. :D/>/>
Expenox #4
Posted 25 November 2012 - 05:08 PM
But how would the "password" or "Password" work out?
Kingdaro #5
Posted 25 November 2012 - 05:17 PM
It wouldn't.

When saying "if this or that then" it's only checking to see if this and that are either true, or if they exist.

In your example, you've provided "if password == this or that". The first one is true, if password == this. However, the second one will always be true, since "that" always exists. The correct way of checking a variable against two values is "if password == this and password == that then"

That brings me to my second point, you're using a while in place of an if. A while basically says "keep doing this code while this condition is true or this variable exists", it'll keep looping the code inside of it. That isn't really what you want. You only want to check if your password is correct and only once.
dissy #6
Posted 25 November 2012 - 05:20 PM
The code above also never asks the user to enter a password.
It prompts them to, but never actually lets them type anything.
Expenox #7
Posted 25 November 2012 - 05:26 PM
It wouldn't. When saying "if this or that then" it's only checking to see if this and that are either true, or if they exist. In your example, you've provided "if password == this or that". The first one is true, if password == this. However, the second one will always be true, since "that" always exists. The correct way of checking a variable against two values is "if password == this and password == that then" That brings me to my second point, you're using a while in place of an if. A while basically says "keep doing this code while this condition is true or this variable exists", it'll keep looping the code inside of it. That isn't really what you want. You only want to check if your password is correct and only once.
Thank you!
The code above also never asks the user to enter a password. It prompts them to, but never actually lets them type anything.
Yeah I saw, like I said I rushed this code, I already have a working door code I just wanted one better and all that.
Expenox #8
Posted 25 November 2012 - 05:46 PM
Thanks for all the help guys!
Expenox #9
Posted 25 November 2012 - 07:25 PM
And ran into another error when creating a startup…
Code:

local password = Expenox
local pass = expenox
print("Welcome to Expocraft v1.0 by Expenox")
sleep(1)
textutils.slowPrint("Wait while Expocraft loads please.")
sleep(1)
textutils.slowPrint("-")
sleep(1)
textutils.slowPrint("--")
sleep(1)
textutils.slowPrint("---")
sleep(1)
textutils.slowPrint("----")
sleep(1)
textutils.slowPrint("-----")
sleep(1)
textutils.slowPrint("Expocraft loaded!")
sleep(4)
while true do
term.write("Enter Password: ")
local input = read("*")
if input == password then
print("Correct password!")
sleep(2)
term.clear()
else
print("Wrong password!")
os.reboot()
end
end
Very simple and all.
But when i enter the correct password it just skips the Correct password, and if i get it wrong just skips the wrong password.
So it basicly runs the os.reboot right or not, help please?
Pastebin: http://pastebin.com/SzGqWf7i
Mitchfizz05 #10
Posted 25 November 2012 - 09:09 PM
I'm not sure about that last problem, but in your last piece of code, I recommend you put "term.setCursorPos(1,1)" right after "term.clear".
Oh and also, have 1 password variable, with its value all lowercase, then after "local input = read("*")" put "input = string.lower(input)". That should make the password not case sensitive. (Sorry if my post is hard to understand =)
Expenox #11
Posted 26 November 2012 - 08:53 AM
I'm not sure about that last problem, but in your last piece of code, I recommend you put "term.setCursorPos(1,1)" right after "term.clear". Oh and also, have 1 password variable, with its value all lowercase, then after "local input = read("*")" put "input = string.lower(input)". That should make the password not case sensitive. (Sorry if my post is hard to understand =)
Thank you!
Expenox #12
Posted 26 November 2012 - 09:05 AM
Sadly, this doesn't fix the problem though. Anyone can help with the loop problem?
Kingdaro #13
Posted 26 November 2012 - 09:17 AM
At the top, you should have quotes around "Exponox".
Expenox #14
Posted 26 November 2012 - 10:52 AM
At the top, you should have quotes around "Exponox".
Oh, can't believe I keep missing the simple things. -_-/>
Expenox #15
Posted 26 November 2012 - 11:08 AM
Oh boy. This time it works but then, it says Correct pass then says welcome. But then asks me to enter the pass again!
code:

local password = "expenox"
print("Welcome to Expocraft v1.0 by Expenox")
sleep(1)
textutils.slowPrint("Wait while Expocraft loads please.")
sleep(1)
textutils.slowPrint("-")
sleep(1)
textutils.slowPrint("--")
sleep(1)
textutils.slowPrint("---")
sleep(1)
textutils.slowPrint("----")
sleep(1)
textutils.slowPrint("-----")
sleep(1)
textutils.slowPrint("Expocraft loaded!")
sleep(4)
while true do
term.write("Enter Password: ")
local input = read("*")
input = string.lower(input)
if input == password then
print("Correct password!")
textutils.slowPrint("Getting account info...")
sleep(2)
term.clear()
term.setCursorPos(1,1)
textutils.slowPrint("Welcome, Expenox")
else
print("Wrong password!")
os.reboot()
end
end
Pastebin
Kingdaro #16
Posted 26 November 2012 - 11:46 AM
Lol, your password stuff is in a loop, just remove the "while true do" and the last "end" and it should work :D/>/>
Expenox #17
Posted 26 November 2012 - 12:44 PM
Lol, your password stuff is in a loop, just remove the "while true do" and the last "end" and it should work :D/>/>/>/>
Thanks for all your help! It now works :D/>/>
Expenox #18
Posted 27 November 2012 - 01:55 PM
Welp, I will post anymore problems I run into here!
Expenox #19
Posted 16 December 2012 - 07:36 AM
Okay, this isn't really a problem but me just needing some help in understanding it, for the turtle I did

if turtle.detectUp() then
turtle.up()
end
What I want to do is so that when it doesn't detect up it goes up, but I don't want to make it like this

if turtle.detectUp() then
turtle.digUp()
else
turtle.up()
end
So any help I would appreciate. :D/>

Also when I just try the first code says i'm missing a '='.
Ulthean #20
Posted 16 December 2012 - 07:48 AM
Try this


if not turtle.detectUp() then
  turtle.up()
end
Expenox #21
Posted 16 December 2012 - 08:25 AM
Try this


if not turtle.detectUp() then
  turtle.up()
end
It just stays there, I gave it coal too.
Ulthean #22
Posted 16 December 2012 - 08:39 AM
What code did you put it in?
Expenox #23
Posted 16 December 2012 - 08:54 AM
What code did you put it in?
The one you told me to try.

Now to think of it, I should add a fuel function…
+ Added Fuel Function(Pastebin)
(Still not done)
Expenox #24
Posted 16 December 2012 - 09:32 AM
Okay it works, but now I need help on how to get it to move more then one time up.
When I run this it only goes up once, when it should loop itself.
Ulthean #25
Posted 16 December 2012 - 09:39 AM
This code loops it endlessly


local function checkFuel()
  if turtle.getFuelLevel() <1 then
	turtle.select(1)
	turtle.refuel(1)
  end
end

while true do
  checkFuel()
  if not turtle.detectUp() then
	turtle.up()
  end
end

This code loops it until there is a block above


local function checkFuel()
  if turtle.getFuelLevel() <1 then
	turtle.select(1)
	turtle.refuel(1)
  end
end

while not turtle.detectUp() do
  checkFuel()
  turtle.up()
end

This code goes up 10 times


local function checkFuel()
  if turtle.getFuelLevel() <1 then
	turtle.select(1)
	turtle.refuel(1)
  end
end

for i=1,10 do
  checkFuel()
  if not turtle.detectUp() then
	turtle.up()
  end
end
Expenox #26
Posted 16 December 2012 - 09:45 AM
This code loops it endlessly


local function checkFuel()
  if turtle.getFuelLevel() <1 then
	turtle.select(1)
	turtle.refuel(1)
  end
end

while true do
  checkFuel()
  if not turtle.detectUp() then
	turtle.up()
  end
end

This code loops it until there is a block above


local function checkFuel()
  if turtle.getFuelLevel() <1 then
	turtle.select(1)
	turtle.refuel(1)
  end
end

while not turtle.detectUp() do
  checkFuel()
  turtle.up()
end

This code goes up 10 times


local function checkFuel()
  if turtle.getFuelLevel() <1 then
	turtle.select(1)
	turtle.refuel(1)
  end
end

for i=1,10 do
  checkFuel()
  if not turtle.detectUp() then
	turtle.up()
  end
end
Yeah just added the while do loop, thanks for the help!
Pastebin
Go here to see the program there and download it, once it's finished.
theoriginalbit #27
Posted 16 December 2012 - 01:29 PM
can i suggest that you do this


input = read(*)
if input:lower() == password then

Did you want it to dig? If so try this.

turtle.detectUp() and turtle.digUp() or turtle.up()

And if you want it to dig, you should probably allow for sand and gravel falling before trying to move again, can cause problems if code isn't done right sometimes :)/>
Expenox #28
Posted 16 December 2012 - 01:39 PM
can i suggest that you do this


input = read(*)
if input:lower() == password then

Did you want it to dig? If so try this.

turtle.detectUp() and turtle.digUp() or turtle.up()

And if you want it to dig, you should probably allow for sand and gravel falling before trying to move again, can cause problems if code isn't done right sometimes :)/>
It is a lumberjack, and no I meant it not to dig.
theoriginalbit #29
Posted 16 December 2012 - 01:43 PM
can i suggest that you do this


input = read(*)
if input:lower() == password then

Did you want it to dig? If so try this.

turtle.detectUp() and turtle.digUp() or turtle.up()

And if you want it to dig, you should probably allow for sand and gravel falling before trying to move again, can cause problems if code isn't done right sometimes :)/>
It is a lumberjack, and no I meant it not to dig.

ok just checking :)/> in your original code you had it digging.

FYI making the string lowercase means that no matter how they enter the string it will work.
Expenox #30
Posted 17 December 2012 - 04:44 AM
can i suggest that you do this


input = read(*)
if input:lower() == password then

Did you want it to dig? If so try this.

turtle.detectUp() and turtle.digUp() or turtle.up()

And if you want it to dig, you should probably allow for sand and gravel falling before trying to move again, can cause problems if code isn't done right sometimes :)/>
It is a lumberjack, and no I meant it not to dig.

ok just checking :)/> in your original code you had it digging.

FYI making the string lowercase means that no matter how they enter the string it will work.
For my password I already did that..
Check here for it…
and for my lock I did that on purpose, experimenting each way.