Home > Mobile >  How to make the if condition contain more than one condition? i.e.: if (number % 3 == 0) & (number %
How to make the if condition contain more than one condition? i.e.: if (number % 3 == 0) & (number %

Time:05-07

Starting_Num = int(input('Please enter intial number: '))

Ending_Num = int(input('Please enter ending number: '))

for number in range(Starting_Num, (Ending_Num 1)):

    if (number % 3 == 0):
        print(number, '--',  3)
    elif (number % 5 == 0):
        print(number, '--', 5)
    elif (number % 3 == 0) & (number % 5 == 0):
        print(number, '-- both')
    elif (number % 3 != 0) & (number % 5 != 0):
        print(number)

CodePudding user response:

you can simply do

if (number % 3 == 0) and (number % 5 == 0):
    your code here...

CodePudding user response:

With the Python syntax, you need to use an and instead of &.

Test if a is greater than b, AND if c is greater than a:

a = 200
b = 33
c = 500
if a > b and c > a:
  print("Both conditions are True")

Source: https://www.w3schools.com/python/python_conditions.asp#:~:text=The and keyword is a logical operator, and is used to combine conditional statements

CodePudding user response:

Does your program error at all? Python 3 allows keyword & as well as and to be used as "and" conditional statements. It should work either way...

CodePudding user response:

You can use "and" or "or" operator, difference between them is that "and" should call True if all of the condition are True, "or" - if one of it is True.

example:

a = 1
b = 2
c = 'd'
if type(a) == int and type(b) == int:
   # that will be printed
   print("a, b are numbers")
if type(a) == int and type(c) == int:
   # that will not be printed
   print("a, c are numbers")
if type(a) == int or type(c) == int:
   # that will be printed
   print("a or c is number")

your code should look like:

Starting_Num = int(input('Please enter intial number: '))

Ending_Num = int(input('Please enter ending number: '))

for number in range(Starting_Num, (Ending_Num 1)):

    if (number % 3 == 0):
        print(number, '--',  3)
    elif (number % 5 == 0):
        print(number, '--', 5)
    elif (number % 3 == 0) and (number % 5 == 0):
        print(number, '-- both')
    elif (number % 3 != 0) and (number % 5 != 0):
        print(number)

CodePudding user response:

The problem is where you position your 'and' condition. In your code, assuming the input fits the condition (e.g. the number is 15) what will happen is that you will enter the first condition: (if (number % 3 != 0):) and skip all the others, because you used [if / elif] statements. What you need to do is move the condition [elif (number % 3 != 0) & (number % 5 != 0):] to be the first condition, which will ensure that in case the number is divisible by both 3 and 5 - you will first check for both conditions. Also, in case of logical and use the and operator.

It should look something like this:

Starting_Num = int(input('Please enter intial number: '))

Ending_Num = int(input('Please enter ending number: '))

for number in range(Starting_Num, (Ending_Num 1)):

if (number % 3 == 0) and (number % 5 == 0):
    print(number, '-- both')
elif (number % 3 == 0):
    print(number, '--',  3)
elif (number % 5 == 0):
    print(number, '--', 5)
else:
    print(number)    
  • Related