0

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.

1 Answer 1

1
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()

Sign up to request clarification or add additional context in comments.

3 Comments

What is the motivation for replacing the default values with None? That works of course, but might leave OP with the impression that it was required to solve the problem. The rest of your answer explains what the true problem is,
I just didn't want to repeat the same definition. That way, whether the function receives "" or nothing, it get defined at the ifs part.
Thanks a lot, both of you. I got how the def function works when the user types nothing, it replaces the default parameter with none. Again thank you a lot!!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.