53 posts
Location
Adelaide, Australia.
Posted 07 March 2013 - 10:58 PM
function login(arg1, arg2)
local user = { "user1", "adam", "aam" }
local pass = { "pass1", "mada", "maa" }
for index, value in ipairs(user) do
if arg1 == v then
password = pass[i]
valid = true
break
else
valid = false
return false
end
end
if valid == true and password == arg2 then
return true
else
return false
end
end
^ The code of my login function…
It with only work with login("user1", "pass1") for every other user it returns false.. WHy?
537 posts
Location
Copenhagen, Denmark
Posted 07 March 2013 - 11:54 PM
You do pass instead of pass[index]
[Unacceptable. -L]
2088 posts
Location
South Africa
Posted 08 March 2013 - 12:25 AM
function login(arg1, arg2)
local user = { "user1", "adam", "aam" }
local pass = { "pass1", "mada", "maa" }
for index, value in ipairs(user) do
if arg1 == value then
return arg2 == pass[index] -- if it's the correct pass, will return true, else false
end
end
return false
end
You mustn't have an else in the loop
818 posts
Posted 08 March 2013 - 01:01 AM
Do you think he has not looked at his code before posting it here, @Mads? Do you think he gone through all the hassle to get this thread going, without looking at his code? Ofcourse he has looked at his code, he doesn't get his head around it, so he comes here and ask other coders if they can get their head around it. If you are not very experienced with lua you might not be that good at spotting "obvious" problems. It might not even be about being unexperienced, but even the fact that you can blind yourself. This is a great place for people to ask other people about problems they don't understand. Even me who could generaly speaking say I know almost everything about lua, even I can't spot errors at times. Luckily, I do know another great lua coder irl so we can have a chat about it and spot the error together. What I want to come to is that a "obvious" error to you might not be so obvious to someone else. Try to look into someone else's thoughts and realise everyone is different.
Long story short, give a polite answer and both parts will feel much better at the end of the day.
1511 posts
Location
Pennsylvania
Posted 08 March 2013 - 01:09 AM
realise everyone is different.
Are you trying to say i'm fat? Because that'd be a.. false statement/accusasion :P/>
2088 posts
Location
South Africa
Posted 08 March 2013 - 01:50 AM
Do you think he has not looked at his code before posting it here, @Mads? Do you think he gone through all the hassle to get this thread going, without looking at his code? Ofcourse he has looked at his code, he doesn't get his head around it, so he comes here and ask other coders if they can get their head around it. If you are not very experienced with lua you might not be that good at spotting "obvious" problems. It might not even be about being unexperienced, but even the fact that you can blind yourself. This is a great place for people to ask other people about problems they don't understand. Even me who could generaly speaking say I know almost everything about lua, even I can't spot errors at times. Luckily, I do know another great lua coder irl so we can have a chat about it and spot the error together. What I want to come to is that a "obvious" error to you might not be so obvious to someone else. Try to look into someone else's thoughts and realise everyone is different.
Long story short, give a polite answer and both parts will feel much better at the end of the day.
Well said :P/>
Shorter way:
function login(arg1, arg2)
local users = { ["user1"] = "pass1", ["adam"] = "mada", ["aam"] = "maa" }
return users[arg1] and arg2 == users[arg1] or false
end
-- Test it with this
while true do
term.clear()
term.setCursorPos( 1, 1 )
write( ' User: ' ) local user = read()
write( ' Password: ' ) local pass = read()
if login(user, pass) then
print( 'Success!' )
else
print( 'Nope!' )
end
sleep(2)
end
24 posts
Location
The End, shooting the ender dragon because fantasy sucks.
Posted 08 March 2013 - 08:11 AM
Shorter way:
function login(arg1, arg2)
local users = { ["user1"] = "pass1", ["adam"] = "mada", ["aam"] = "maa" }
return users[arg1] and arg2 == users[arg1] or false
end
-- Test it with this
while true do
term.clear()
term.setCursorPos( 1, 1 )
write( ' User: ' ) local user = read()
write( ' Password: ' ) local pass = read()
if login(user, pass) then
print( 'Success!' )
else
print( 'Nope!' )
end
sleep(2)
end
I'd expect this way would work alot better.
1511 posts
Location
Pennsylvania
Posted 08 March 2013 - 09:17 AM
I'd expect this way would work alot better.
Well duh, it was made by remiX. Why wouldn't it be better :P/>
24 posts
Location
The End, shooting the ender dragon because fantasy sucks.
Posted 08 March 2013 - 11:40 AM
/Well duh, it was made by remiX. Why wouldn't it be better :P/>
rofl
1511 posts
Location
Pennsylvania
Posted 08 March 2013 - 12:03 PM
@Roadhouse699:
You #%@&!, why would you try to kill the Ender Dragon! She is a majestic piece of art. How dare you use such careless words!
997 posts
Location
Wellington, New Zealand
Posted 08 March 2013 - 01:08 PM
function login(arg1, arg2)
local user = { "user1", "adam", "aam" }
local pass = { "pass1", "mada", "maa" }
for index, value in ipairs(user) do
if arg1 == v then
password = pass[i]
valid = true
break
else
valid = false
return false
end
end
if valid == true and password == arg2 then
return true
else
return false
end
end
^ The code of my login function…
It with only work with login("user1", "pass1") for every other user it returns false.. WHy?
Because you return false if the username they entered doesn't match the username you're checking. Return statements immediately exit the function, so your function returns after checking the first user and doesn't check any of the others.
Also I think you wrote "v" instead of "value" and "i" instead of "index" when you wrote this code on the forums, or vice versa.
24 posts
Location
The End, shooting the ender dragon because fantasy sucks.
Posted 08 March 2013 - 01:16 PM
@Roadhouse699:
You #%@&!, why would you try to kill the Ender Dragon! She is a majestic piece of art. How dare you use such careless words!
Wow, I'm not the only person who thinks the ender dragon is a she.
But piece of art… what are you, attracted to her?
1214 posts
Location
The Sammich Kingdom
Posted 08 March 2013 - 03:22 PM
Wow, I'm not the only person who thinks the ender dragon is a she.
She drops a egg when you kill her. How can she not be a female unless a male dragon shits out a egg.
2005 posts
Posted 08 March 2013 - 03:35 PM
Well, the egg is a death drop, which could mean that the male is carrying the egg to protect it. Some species do that.
Though if you don't take the position that the Endermen are actually female, then it makes sense for the Enderdragon to be one.
1511 posts
Location
Pennsylvania
Posted 08 March 2013 - 04:22 PM
Ok, the Endermen are male; they carry their reproductive sacs along with them whilst they travel. The Ender Dragon is female; she gave birth to all the Endermen in the End dimension. I am her marido! Her and I watch over the End keeping that damn "Steve" character from destroying the precious Ender Cystals. When "Steve" is not around, I code EnderOS on top of the highest pillar in the dimmest light (Like I said before). There is my home; my grave; my cynosure. The Endermen; my children were sent to the Overworld in search of the "Steve". They managed to get help from the silverfish which now guard the entrance to the third dimension. For now we can say it is safe; but he will show his face again; his pixelated fists will avenge…
53 posts
Location
Adelaide, Australia.
Posted 08 March 2013 - 06:29 PM
Thank you guys for replying… I did finally work out why….
I have to thank
Doyle3694 for replying to that unnecessary comment. I will post my code below but please ask that no one directly copies it for reasons such as the code is valid within an os I know that it can be broken easy.
function login(arg1, arg2)
for i, v in ipairs(user) do
if arg1 == v then
password = pass[i]
break
end
end
if arg2 == password then
return true
else
return false
end
end
7508 posts
Location
Australia
Posted 08 March 2013 - 06:43 PM
Thank you guys for replying… I did finally work out why….
Ok big suggestion… this
if arg2 == password then
return true
else
return false
end
make it this
return arg2 == password
an if statement's condition always comes down to true/false, so there is no point using an if statement to return a boolean, just return the condition :)/>
53 posts
Location
Adelaide, Australia.
Posted 08 March 2013 - 06:46 PM
Thanks mate.
7508 posts
Location
Australia
Posted 08 March 2013 - 06:47 PM
no problems.
24 posts
Location
The End, shooting the ender dragon because fantasy sucks.
Posted 09 March 2013 - 02:23 AM
Ok, the Endermen are male; they carry their reproductive sacs along with them whilst they travel. The Ender Dragon is female; she gave birth to all the Endermen in the End dimension. I am her marido! Her and I watch over the End keeping that damn "Steve" character from destroying the precious Ender Cystals. When "Steve" is not around, I code EnderOS on top of the highest pillar in the dimmest light (Like I said before). There is my home; my grave; my cynosure. The Endermen; my children were sent to the Overworld in search of the "Steve". They managed to get help from the silverfish which now guard the entrance to the third dimension. For now we can say it is safe; but he will show his face again; his pixelated fists will avenge…
Cool. My job is to install flan's mod, make a content pack with stinger missiles, get stinger missiles in the game as well as some other guns, kill a whole bunch of endermen and blazes, find a stronghold, go to the end, and shoot the ender dragon with a stinger missile, as well as ward off all you endermen and shoot the ender crystals.
7508 posts
Location
Australia
Posted 09 March 2013 - 02:31 AM
Cool. My job is to install flan's mod, make a content pack with stinger missiles, get stinger missiles in the game as well as some other guns, kill a whole bunch of endermen and blazes, find a stronghold, go to the end, and shoot the ender dragon with a stinger missile, as well as ward off all you endermen and shoot the ender crystals.
I see your Flan's Mod missiles and raise you the ICBM Enderman Missile.
24 posts
Location
The End, shooting the ender dragon because fantasy sucks.
Posted 09 March 2013 - 04:50 AM
Cool. My job is to install flan's mod, make a content pack with stinger missiles, get stinger missiles in the game as well as some other guns, kill a whole bunch of endermen and blazes, find a stronghold, go to the end, and shoot the ender dragon with a stinger missile, as well as ward off all you endermen and shoot the ender crystals.
I see your Flan's Mod missiles and raise you the ICBM Enderman Missile.
I'm not anti-ender stuff, just anti-fantasy.