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
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.
Aucun commentaire:
Enregistrer un commentaire