ICG
ICG

Reputation: 185

Elseifs being skipped in Lua

I started to work on a little Game-Dev project in Lua, and am having trouble with this part of my code:

    if genrel == RPG and langl == BASIC and topicl == WAR then
        review = math.random(2, 5)
        review2 = math.random(2, 5)
        review3 = math.random(2, 3)
        money = money + 300
        print("You have earned a total of $300 dollars from your game. Overall not many people enjoyed the game.")

elseif genrel == RPG and langl == BASIC and topicl == "WESTERN" then
        review = math.random(7, 9)
        review2 = math.random(4, 9)
        review3 = math.random(5, 8)
    money = money + 400
    print("You have earned a total of $300 dollars from your game. The game recieved mixed reviews.")

topicl, langl, and genrel, are defined earlier in the code. Example:

topicChoice = io.read()
if topicChoice == 'War' then
topic = "[War]"
topicl = WAR

progLang = io.read()
if progLang == 'Java' then
lang = "[JAVA]"
langl = JAVA

genreChoice = io.read()
if genreChoice == 'ACTION' then
genre = "[ACTION]"
genrel = ACTION

Everything is defined, but when I run the code the random numbers outputted are the first ones under the if, no matter what I put in. This may be hard to understand, so here is my full code. http://pastebin.com/XS3aEVFS

Summary: The program decides what random numbers are shown by determining what the genre, topic, and coding language are. Instead of selecting the numbers by genre, topic, and coding language, it simply uses the first if statement.

Upvotes: 2

Views: 115

Answers (2)

greatwolf
greatwolf

Reputation: 20878

Lorenzo covered the main point of why your code doesn't execute as you expect. A secondary issue is that you're checking against a string inputted by the player but you're not normalizing the case.

Consider what happens if the player enters something like WeSTErn. That is not the same as WESTERN -- your variables don't get set correctly and your program outputs the wrong results again.

Either normalize player input before comparing them, by using either string.upper or string.lower, or use a different data type, eg. numbers. Not everything has to be represented as a string when working data.

Should I put quotes around each in the if statements like Krister Andersson said?

Only if you expect those variables to hold string types. You can just as well assign each of them unique numbers for identifying between them. Something like this for instance:

local ACTION, RPG, SIM = 1, 2, 3
local JAVA, BASIC = 1, 2, 3
local WAR, WESTERN, BLOCKS = 1, 2, 3
-- etc.

Final note, you should really consider breaking up your program -- that's why functions were invented.

Upvotes: 1

Early in your code you have this:

if genreChoice == 'ACTION' then
    genre = "[ACTION]"
    genrel = ACTION     
elseif genreChoice == 'RPG' then
    genre = "[RPG]"
    genrel = RPG     
elseif genreChoice == 'SIM' then
    genre = "[SIM]"
    genrel = SIM     
end

and you assign to genrel the value of the variables ACTION, RPG and SIM, but those variables don't appear to have been defined anywhere, thus their value is nil. In other words when you execute:

genrel = ACTION     

is the same as if you did:

genrel = nil     

Upvotes: 2

Related Questions