Josephi Dmitry
Josephi Dmitry

Reputation: 61

Python - Multiple Elif Statements Being Skipped Over in If Statement

I'm trying to get my program to return different statements based off the hour the user inputs. If I input a number for hour between he first two statements ((hours < 6) and (hours <= 10) or (hours >= 6)), it will return the correct string but if I input anything greater than 10 for the hour, it won't return the intended string for that hour but it will keep repeating the second string.

Any help is appreciated!

Here's my program: https://i.sstatic.net/uQzBi.png

def food(hours, boolean):

    if boolean == "True" or boolean == "true":        
        if (hours < 6):
            return "no food"

        elif (hours <= 10) or (hours >= 6):
            return "breakfast, marmalade"

        elif (hours <= 15) or (hours >= 11): 
            return "lunch, true,dessert"

        elif (hours < 22) or (hours >= 15):
            return "dinner, dessert"

        else:
            return "no food"

    else:
        if (hours < 6):
            return "no food"

        elif (hours <= 10) or (hours >= 6):
            return "breakfast,coffee"

        elif (hours <= 15) or (hours >= 11):
            return "lunch, false"

        elif (hours < 22) or (hours >= 15):
            return "dinner"

        else:
            return "no food"

x = food(15, "true")
print(x)

Upvotes: 1

Views: 1965

Answers (4)

Prayson W. Daniel
Prayson W. Daniel

Reputation: 15568

Python have boolean value True and False. Their is no need to use strings 'True' or 'False'. You can also use the power of if-elif-else logic. Python executes from top to bottom, when condition is met, it breaks. Your function can be rewritten to this:

def food(hour, boolean):
    '''Food 
    Takes in hour as int and boolean as bool
    E.g. x = food(15,True)
    # TODO:
    Ensure that input data types are correct.
    '''
    if boolean:
        if hour >= 22 or hour >= 0:
            return 'no food'
        elif hour >= 15:
            return 'dinner, dessert'
        elif hour >= 11:
            return 'lunch, true,dessert'
        elif hour >= 6:
            return 'breakfast, marmalade'
        else:
            raise ValueError('something wrong')
    else:
        if hour >= 22 or hour >= 0:
            return 'no food'
        elif hour >= 15:
            return 'dinner'
        elif hour >= 11:
            return 'lunch, false'
        elif hour >= 6:
            return 'breakfast, coffee'
        else:
            raise ValueError('something wrong')


x = food(15, True)
print(x)

Upvotes: 1

evantkchong
evantkchong

Reputation: 2606

Welcome to StackOverflow! As mentioned by the other answers, using 'and' instead of 'or' would solve your issue. However, it is redundant to include more than one condition for each meal if they are all sequential as by writing:

if (hours < 6):
    return "no food" 

you are already saying to only output the return value if the hour input is less than 6, hence only values more than 6 would make it to the next elif statement.

Do let me know if I misunderstood something about your program's use case that required you to write the code as such!

Upvotes: 0

TonyRyan
TonyRyan

Reputation: 328

Looks like the first elif statement is your problem. You should use and instead of or. By using or, anything >= 6 will return breakfast marmalade, not just anything between 6 and 10.

Upvotes: 0

Ryan Cogswell
Ryan Cogswell

Reputation: 80986

You should be using “and” instead of “or”. Anything > 10 will also be >= 6 so the second condition always matches.

Upvotes: 3

Related Questions