'If-else in Python [closed]

Fix this:

for n in range(1,8): 
    print(n) 
    if(n>=2 & n<=5): 
        print("This Number is either 2,3,4 or 5\n")
    else: 
        print("This Number is "+n)


Solution 1:[1]

Use and. and is a Logical AND that returns True if both the operands are true whereas & is a bitwise operator in Python

for n in range(1,8): 
    print(n) 
    if(n >= 2 and n <= 5): 
        print("This Number is either 2,3,4 or 5\n") 
    else: 
        print("This Number is ", + n)

Solution 2:[2]

1. & is bitwise and operator while and is used for logical and operator.
2. You need to convert integer to string for concatenation.

Corrected code:

for n in range(1,8): 
    print(n) 
    if(n>=2 and n<=5): 
        print("This Number is either 2,3,4 or 5\n")
    else: 
        print("This Number is "+str(n))

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 logi
Solution 2 Python learner