r/PythonLearning • • Aug 14 '25

Why I didn't getting default value

Post image
5 Upvotes

18 comments sorted by

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.

1

u/Red_Priest0 Aug 14 '25

I want default value when there is no input from user

11

u/rebelle3 Aug 14 '25

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

3

u/Much_Buy7605 Aug 14 '25

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

3

u/niket23697 Aug 14 '25

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

2

u/SCD_minecraft Aug 14 '25
A = input()
print(A or "world")

If user inputs anything, it will print that

If user inputs nothing, it will print world

0

u/Red_Priest0 Aug 14 '25

I want default when there is no input from user

1

u/PureWasian Aug 14 '25

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)

1

u/brasticstack Aug 14 '25
def hello(to=None):
    print('Hello ', to or 'World')

0

u/shlepky Aug 14 '25 edited Aug 14 '25

Change the comma to + in the print function call.

1

u/JeLuF Aug 14 '25

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.

1

u/shlepky Aug 14 '25

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.

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

u/Dramatic-Drawing-844 Aug 14 '25
if name:
    hello(name)
else:
    hello()

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

u/Ooblahnooblah Aug 15 '25

Call hello()

1

u/KOALAS2648 Aug 15 '25

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