Jump to content




help creating program


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

#1 riley5678

  • New Members
  • 1 posts

Posted 23 June 2013 - 04:05 PM

hi i am making a program for a hotel me and friends have built but its not working right and im not sure why am i doing something wrong
ive put the code in the spoiler sorry its a bit long

Spoiler

Edited by Lyqyd, 23 June 2013 - 08:00 PM.
added code tags


#2 Lyqyd

    Lua Liquidator

  • Moderators
  • 8,465 posts

Posted 23 June 2013 - 07:59 PM

Split into new topic.

#3 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 23 June 2013 - 08:15 PM

hello riley5678,

In the future, please post the error that you're getting from the computer, if you're getting one, they are very useful and tell you a great deal of information about your problem. That information helps us, help you, much quicker.

In this case you obviously are not getting an error (based on what I can see is wrong), so in that case please post what you think the program should be doing, and what it actually is doing. Even point us to a specific section if you think the problem is there.

The immediate problem that I can see is with all your if conditionals

if inputc == "no" or "No" or "N" or "n" then


In Lua a value will resolve to true, and nil will resolve to false. As such even when your conditionals are being evaluated this is the result

true/false or true or true or true

And by the rules of boolean logic the outcome of this condition, irrelevant of the first value whether it is true or false, the result will be true, meaning that the if statement will run. Why? This is boolean logic.

OR
true or true = true
true or false = true
false or true = true
false or false = false

AND
true and true = true
true and false = false
false and true = false
false and false = false

NOT
not true = false
not false = true

As such we must change the if statements so that they accurately check the variable, to do this we add "inputc == " before each value, resulting in

if inputc == "no" or inputc == "No" or inputc == "N" or inputc == "n" then

However we can further improve this logic by doing the following

local inputc = string.lower(read())
if inputc == "no" or inputc == "n" then

Notice how just by making the input lowercase we dramatically shorten the conditional? Much better isn't it.

Lastly, another thing that is not so much a bug, or a problem, but definitely something that can be improved is your if statement structures. Currently you're doing this

if (condition) then
  -- code
end


if (condition) then
  -- code
end


if (condition) then
  -- code
end


if (condition) then
  -- code
end

The more efficient way to do this, when checking the same variable over and over, is to use the `elseif` keyword like so

if (condition) then
  -- code

elseif (condition) then
  -- code

elseif (condition) then
  -- code

elseif (condition) then
  -- code
end

This way if it matches one, it will perform it, and not bother checking the rest, because we know it won't match, and if it doesn't match it will just move on and check the next one. Defining one final `else` will also give a way of saying, "well it was none of these"

if (condition) then
  -- code

elseif (condition) then
  -- code

elseif (condition) then
  -- code

elseif (condition) then
  -- code
else
  print("Unknown command")
end

I hope this helps and if you have any questions just ask :)

— BIT





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users