1

I am fairly new to python, and I can't figure out if this is possible. I have a function where I want to check conditions, but continue checking the rest of the conditions even when one is true. For example:

    def function(n):
        if n % 2 == 0:
            print("Number is even")
        if n >= 10:
            print("Number is at least two digits")
        if n % 3 == 0:
            print("3 is a divisor of number")

If I were to input a number like 9, the output would be:

3 is a divisor of number

but if I put in 30, it would print:

Number is even

rather than my desired output of all three statements:

Number is even

Number is at least two digits

3 is a divisor of number

I know that I can't do this:

    def function(n):
        if n % 2 == 0:
            print("Number is even")
            if n >= 10:
                print("Number is at least two digits")
                if n % 3 == 0:
                    print("3 is a divisor of number")

because an input of a number like 15 would output nothing, instead of the two statements that apply to it.

I have researched quite a bit, and there seems to be no other related questions or articles that are easy to find. Again, I am new to python(and programming in general, have been slowly learning for about a month) and there may be some terminology that I do not know to search. The most similar posts on here I could find were:

Can I continue an IF-ELSE statement through a function?

and

https://stackoverflow.com/questions/36757965/how-to-have-multiple-conditions-for-one-if-statement-in-python[2]

as well as many other websites trying to only explain if, elif, and else statements to me, like Python’s cascaded if statement: test multiple conditions after each other but with no clarification on whether or not this is possible. Any help would be appreciated, or an alternative method to getting around this restriction.

  • This is a really stupid question, but please do not downvote it so I lose my posting privilege. I'm new and just trying to learn. I apologize for not testing out toned down code first, it didn't even cross my mind. –  Oct 01 '21 at 20:48

1 Answers1

0

Your code already does exactly what you want:

In [1]: def function(n):
   ...:     if n % 2 == 0:
   ...:         print("Number is even")
   ...:     if n >= 10:
   ...:         print("Number is at least two digits")
   ...:     if n % 3 == 0:
   ...:         print("3 is a divisor of number")
   ...:

In [2]: function(30)
Number is even
Number is at least two digits
3 is a divisor of number

In [3]: function(15)
Number is at least two digits
3 is a divisor of number

In [4]: function(9)
3 is a divisor of number
ddejohn
  • 8,775
  • 3
  • 17
  • 30
  • Thanks. I accidentally reversed my thought process while writing that test code, as I was simplifying a much larger function to make it easier. Is the proper thing to do to edit my question or to create a new one? –  Oct 01 '21 at 20:24
  • Go ahead and edit your current post. – ddejohn Oct 01 '21 at 20:25
  • Agh nevermind it must be some other error within my function. Everything seems to be working when I dumb it down. I guess I know to try that in the future before posting here. Thanks for the help though. I guess deleting my question is the best thing to do now. I don't want to lost all of my 9 rep from this. –  Oct 01 '21 at 20:32