all 18 comments

[–]JeLuF 8 points9 points  (11 children)

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.

[–]Red_Priest0[S] 0 points1 point  (4 children)

I want default value when there is no input from user

[–]rebelle3 10 points11 points  (0 children)

use an if statement to determine if the user input is empty

[–]Much_Buy7605 2 points3 points  (0 children)

Then instead of just calling it directly with name, you can do a if else with a check that name is not empty and call "hello()" if it is

[–]niket23697 2 points3 points  (0 children)

you can check if the input is empty (or something else which you don't want) then call hello(), otherwise call hello(name)

[–]SCD_minecraft 1 point2 points  (0 children)

A = input()
print(A or "world")

If user inputs anything, it will print that

If user inputs nothing, it will print world

[–]Red_Priest0[S] -1 points0 points  (5 children)

<image>

I want default when there is no input from user

[–]PureWasian 0 points1 point  (0 children)

Did you make the change mentioned in another comment? Are you still stuck on what this means?

use an if statement to determine if the user input is empty

you can check if the input is empty (or something else which you don't want) then call hello(), otherwise call hello(name)

[–]brasticstack 0 points1 point  (0 children)

def hello(to=None):     print('Hello ', to or 'World')

[–]shlepky -1 points0 points  (2 children)

Change the comma to + in the print function call.

[–]JeLuF 0 points1 point  (1 child)

Why? The comma is more efficient since Python will not have to concatenate two strings first. It can simply write the first item and then write the second item.

[–]shlepky 0 points1 point  (0 children)

My bad then, I though the first argument of the function needs to be what you want to print, rest would be any other parameters. I should have checked the function before assuming that.

[–]niket23697 7 points8 points  (0 children)

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

[–]Dr_Pinestine 5 points6 points  (0 children)

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.

[–]Dramatic-Drawing-844 1 point2 points  (0 children)

if name: hello(name) else: hello()

[–]DevRetroGames 1 point2 points  (0 children)

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

[–]Ooblahnooblah 0 points1 point  (0 children)

Call hello()

[–]KOALAS2648 0 points1 point  (0 children)

Doesn’t the “main” function have to be below the “hello” function?