Home > Software design >  The default values of a function on Python is not working
The default values of a function on Python is not working

Time:12-12

I'm starting to study Python functions, and I have a problem that it seems correct to me but it isn't working.

I want to show two informations typed. Jogador (player) and Gols (Score), when I type the 2 inputs it works very well, but I want to have two default parameters and this is not working, can someone help me, what am I doing wrong? `

def ficha(nome="<desconhecido>", gols=0):
    return f'O jogador {nome} fez {gols} gol(s) no campeonato.'


jog = input('Nome do jogador: ')
gol = input('Número de gols: ')
print(ficha(jog, gol))

`

I'm expecting to shows the default parameters when nothing is typed by the user.

CodePudding user response:

def ficha(nome=None, gols=None):
    if not nome:
        nome = "<desconhecido>"
    if not gols:
        gols =0
    return f'O jogador {nome} fez {gols} gol(s) no campeonato.'


jog = input('Nome do jogador: ')
gol = input('Número de gols: ')
print(ficha(jog, gol))

When you don't enter anything to the input, it saves "". When you send it to the function it gets "" and replace it with the default arguments. If you want your function to use the default parameters, just don't send them to the function.

Use: ficha()

  • Related