8
u/niket23697 Aug 14 '25
an empty string is still a parameter. to be able to use the default value of 'to', you'll need to call hello() without any parameters. you can implement that logic in your code
7
u/Dr_Pinestine Aug 14 '25
Today we learn the important difference between zero and null.
If the user types nothing, then input(...) returns an empty string (""), which is then passed as a parameter to hello(...).
Python considers the empty string to be something rather than nothing, so it overrides the default value in the function. If you want it to work as intended, you need to explicitly check for an empty string.
2
2
u/DevRetroGames Aug 15 '25
def _get_user_input(msg: str) -> str:
return input(msg)
def _hello(to: str = "world") -> None:
print(f"hello {to}")
def main() -> None:
to:str = _get_user_input("What is your name? : ")
_hello(to)
main()1
1
8
u/JeLuF Aug 14 '25
Because you call the function with a parameter. Even an emtpy string is still a parameter passed to the function.
Only if you call the function without a parameter, the default would kick in. Try
hello()instead to see how that changes the output.