Jump to content




Loop error?


29 replies to this topic

#1 Expenox

  • Members
  • 137 posts
  • LocationIn the depths of my computer.

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()


#2 Kingdaro

    The Doctor

  • Members
  • 1,636 posts
  • Location'MURICA

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.

#3 Expenox

  • Members
  • 137 posts
  • LocationIn the depths of my computer.

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/>

#4 Expenox

  • Members
  • 137 posts
  • LocationIn the depths of my computer.

Posted 25 November 2012 - 05:08 PM

But how would the "password" or "Password" work out?

#5 Kingdaro

    The Doctor

  • Members
  • 1,636 posts
  • Location'MURICA

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.

#6 dissy

  • Members
  • 181 posts

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.

#7 Expenox

  • Members
  • 137 posts
  • LocationIn the depths of my computer.

Posted 25 November 2012 - 05:26 PM

View PostKingdaro, on 25 November 2012 - 05:17 PM, said:

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!

View Postdissy, on 25 November 2012 - 05:20 PM, said:

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.

#8 Expenox

  • Members
  • 137 posts
  • LocationIn the depths of my computer.

Posted 25 November 2012 - 05:46 PM

Thanks for all the help guys!

#9 Expenox

  • Members
  • 137 posts
  • LocationIn the depths of my computer.

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

#10 Mitchfizz05

  • Members
  • 125 posts
  • LocationAdelaide, Australia

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 =)

#11 Expenox

  • Members
  • 137 posts
  • LocationIn the depths of my computer.

Posted 26 November 2012 - 08:53 AM

View Postmitchfizz05, on 25 November 2012 - 09:09 PM, said:

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!

#12 Expenox

  • Members
  • 137 posts
  • LocationIn the depths of my computer.

Posted 26 November 2012 - 09:05 AM

Sadly, this doesn't fix the problem though. Anyone can help with the loop problem?

#13 Kingdaro

    The Doctor

  • Members
  • 1,636 posts
  • Location'MURICA

Posted 26 November 2012 - 09:17 AM

At the top, you should have quotes around "Exponox".

#14 Expenox

  • Members
  • 137 posts
  • LocationIn the depths of my computer.

Posted 26 November 2012 - 10:52 AM

View PostKingdaro, on 26 November 2012 - 09:17 AM, said:

At the top, you should have quotes around "Exponox".
Oh, can't believe I keep missing the simple things. -_-

#15 Expenox

  • Members
  • 137 posts
  • LocationIn the depths of my computer.

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

#16 Kingdaro

    The Doctor

  • Members
  • 1,636 posts
  • Location'MURICA

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/>

#17 Expenox

  • Members
  • 137 posts
  • LocationIn the depths of my computer.

Posted 26 November 2012 - 12:44 PM

View PostKingdaro, on 26 November 2012 - 11:46 AM, said:

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/>

#18 Expenox

  • Members
  • 137 posts
  • LocationIn the depths of my computer.

Posted 27 November 2012 - 01:55 PM

Welp, I will post anymore problems I run into here!

#19 Expenox

  • Members
  • 137 posts
  • LocationIn the depths of my computer.

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 '='.

#20 Ulthean

  • Members
  • 171 posts

Posted 16 December 2012 - 07:48 AM

Try this

if not turtle.detectUp() then
  turtle.up()
end






1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users