Home > Software design >  If-else in Python
If-else in Python

Time:03-27

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)

CodePudding user response:

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)

CodePudding user response:

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))
  • Related