Home > Net >  how to write return function correct way without print return value?
how to write return function correct way without print return value?

Time:01-20

def function(n):

if n % 2 != 0:
    print('weird')
elif n % 2 == 0 and n in range(2, 5):
    print('not weird')
elif n % 2 == 0 and n in range(6, 20):
    print('weird')
elif n % 2 == 0 and n > 20:
    print('not weird')
return n

while True:
    n = int(input('enter the number: '))

    print(function(n))

OUTPUT:

enter the number: 4
not weird
4

Above code, I don't want to print return number 4 again, how to write without representing the return number?

CodePudding user response:

Change print(function(n)) to function(n)

def function(n):
    if n % 2 != 0:
        print('weird')
    elif n % 2 == 0 and n in range(2, 5):
        print('not weird')
    elif n % 2 == 0 and n in range(6, 20):
        print('weird')
    elif n % 2 == 0 and n > 20:
        print('not weird')
    return n
while True:
    n = int(input('enter the number: '))

    function(n)

CodePudding user response:

i mean u could just remove the print func at the end like so

def function(n):

if n % 2 != 0:
    print('weird')
elif n % 2 == 0 and n in range(2, 5):
    print('not weird')
elif n % 2 == 0 and n in range(6, 20):
    print('weird')
elif n % 2 == 0 and n > 20:
    print('not weird')
return n

while True:
    n = int(input('enter the number: '))
    function(n)

CodePudding user response:

You can also just return from the function and not print it.

CodePudding user response:

you can use it this way.. remove the return from the function statement and also remove the print when calling this function.. If you only remove the return but still call the function using print(func(n)) then the output would be something like

enter the number: 4
not weird
None

Maybe this is what you are looking for?

def function(n):
if n % 2 != 0:
    print('weird')
elif n % 2 == 0 and n in range(2, 5):
    print('not weird')
elif n % 2 == 0 and n in range(6, 20):
    print('weird')
elif n % 2 == 0 and n > 20:
    print('not weird')

while True:
    n = int(input('enter the number: '))
    function(n)
  •  Tags:  
  • Related