Function returning None, when if or Else runs

Asked

Viewed 48 times

0

How do I make the following function not return None?

def dif(x, z):
    if x != z:
        print('\nNúmeros diferentes')
        return
    else:
        print('\nNúmeros iguais')
        return


n1 = int(input('Um número: '))
n2 = int(input('Outro número: '))

print(dif(n1, n2))

1 answer

4

The way the function was written, it returns no value, so the None. For her to return to string that you want to show, do:

def dif(x, z):
    if x != z:
        return '\nNúmeros diferentes';
    else:
        return '\nNúmeros iguais';


n1 = int(input('Um número: '))
n2 = int(input('Outro número: '))

print(dif(n1, n2))

Or else, do a function void, that returns no value, and that only shows the string on the screen, and call the function otherwise:

def dif(x, z):
    if x != z:
        print('\nNúmeros diferentes')
    else:
        print('\nNúmeros iguais')


n1 = int(input('Um número: '))
n2 = int(input('Outro número: '))

dif(n1, n2)

See working on repl.it

  • 1

    Before reaching the first final point I sent the vote of usefulness.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.