Python def general help

Here’s my code:

def gas():
____if 3 or 2 == 4:
________sag_1 = "six"
____elif 3 or 2 == 3:
________sag_1 = "five"
____elif 3 or 2 == 2:
________sag_1 = "four"
____elif 3 or 2 == 1:
________sag_1 = "three"
____return sag_1
sag1 = gas()
print(sag1)

note: had to use underscores to make indentations because when I post the code, I lose the indentations

My questions are: why is the result six? And how do I fix it (logically, it should be “five”)?

Can you explain how you think this expression works?

1 Like

But also, I have updated your post to place you code inside code blocks. So you can remove the underscores since the indentations will remain

When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (').

Can I explain how I think 3 or 2 == 4: works?
Not really… perhaps this is why I don’t understand.
Here’s what I see:
The line, ‘if 3 or 2 == 4:’ says to me, if the number 3 or 2 equivalates to the number 4 (which it does not)… then, the next part is like a command that follows.
In this particular case, I wrote the variable (sag_1) is equal to the string “six”. (ie., sag_1 = “six”).
But since 3 or 2 does not equivalate to 4, then… I assume the next line is read… until the elif statement works. And I was actually trying to figure out how it decided which if statement to prioritize (if it chose == 3, or == 2).
Obviously, I have a fundamental misunderstanding that I need help to clarify, and I don’t even know where to start looking for help (yes, I tried googling where to look for help with python).

Let me break down what your code here is saying

if 3 or 2 == 4:

When you are working with the or operator, it will evaluate each operand separately.
So in this case, 3 is an operand and 2 == 4 is an operand.

For the first operand (3), that will evaluate to True because any non zero integer will be True in Python.

For the second operand, 2 ==4, that will result in False because they are clearly not equal.

So this is what your condition really looks like

if True of False

That will always evaluate to True and that is why six is being printed.

So what you what to do is update all of your if and elif statements.

What you need to say is if num is equal to 4 or if num2 is equal to 4.

hope that helps

1 Like

Thank you jwilkins,

    def gas():
        if 3 == 4 or 2 == 4:
            sag_1 = "six"
        elif 3 == 3 or 2 == 3:
            sag_1 = "five"
        elif 3 == 2 or 2 == 2:
            sag_1 = "four"
        elif 3 == 1 or 2 == 1:
            sag_1 = "three"
        return sag_1
    sag1 = gas()
    print(sag1)
# returns "five"

    def gaso():
        if 3 == 1 or 2 == 1:
            osag_1 = "six"
        elif 3 == 2 or 2 == 2:
            osag_1 = "five"
        elif 3 == 3 or 2 == 3:
            osag_1 = "four"
        elif 3 == 4 or 2 == 4:
            osag_1 = "three"
        return osag_1
    osag1 = gaso()
    print(osag1)
# also returns "five"

This answers my original question, too, before I ran into my fundamental error.

Glad to help :+1:

Out of curiosity, what is the purpose of the gas and gaso functions?
or are these practice functions to learn more about if/elif/else statements?

I’m still grappling with the fundamentals while working to build an arithmetic formulator.

Yes, the gas and gaso functions were just practice to check my understanding, but it was also related to putting dashes at the bottom of each problem (in the arithmetic formulator)… I presume the dashes must match the width of the problem… which depends the length of the addends.

I wanted to check how a def function would return in a situation where multiple elifs were “true” ( true by my logic… lol) and whether and how the order of the if/elif statements mattered (if == 1, elif == 2… vs if == 4, elif == 3…)

My actual code (now, thanks to your help) looks like this:

    def Line_1():
        if len(num[0]) == 4 or len(num[2]) == 4:
            line_1 = "------"
        elif len(num[0]) == 3 or len(num[2]) == 3:
            line_1 = "-----"
        elif len(num[0]) == 2 or len(num[2]) == 2:
            line_1 = "----"
        elif len(num[0]) == 1 or len(num[2]) == 1:
            line_1 = "---"
        return line_1
    line1 = Line_1()

I will have up to five lines I suppose.

So, I finished my arithmetic formatter project today, and it works perfecly in pycharm, but it doesn’t pass the camp test.
I was wondering if I could run it past you or anyone that works for freecodecamp, to get some feedback.
I’m pretty happy with it, even though (full disclosure) I know it’s far from elegant (and probably not even close to what was expected). There’s like a thousand lines of code here… but like I said, it works perfectly in pycharm.

It’s best to make a separate topic for it. There’s lots of ways to make a result that’s close but not exactly what the instructions ask for.

JeremyLT,
Thanks, but I finally got it to pass. However, I do believe it’s worthy of the hall of fame for being some of the worst code ever! :rofl:

I’d encourage you to try to fix it up before moving on if you think it’s bad code.

JeremyLT,
There’s really no “fixing” it- it’s 1053 lines. I looked around and saw other arithmetic formatter codes are typically less than 50 lines (nice!). So, it would be like fixing a battle ship by making a canoe out of a tarp.
I will continue to learn, of course, and will test other codes later, but this thing that I created is done.

Refactoring code to meet the same functionality while being simpler, clearer, and more maintainable is a very important on the job skill. I understand not wanting to do it, but you’re definitely abandoning a opportunity to build critical professional programming skills.

Okay, I get that. I’ve literally only been learning code for about 3 weeks… so as I learn more, I will revisit it.