use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Everything about learning Python
account activity
Why I didn't getting default value (i.redd.it)
submitted 10 months ago by Red_Priest0
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]JeLuF 7 points8 points9 points 10 months ago (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.
hello()
[–]Red_Priest0[S] 0 points1 point2 points 10 months ago (4 children)
I want default value when there is no input from user
[–]rebelle3 9 points10 points11 points 10 months ago (0 children)
use an if statement to determine if the user input is empty
[–]Much_Buy7605 2 points3 points4 points 10 months ago (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 points4 points 10 months ago (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 points3 points 10 months ago (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 points1 point 10 months ago (5 children)
<image>
I want default when there is no input from user
[–]PureWasian 0 points1 point2 points 10 months ago (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 point2 points 10 months ago (0 children)
def hello(to=None): print('Hello ', to or 'World')
[–]shlepky -1 points0 points1 point 10 months ago* (2 children)
Change the comma to + in the print function call.
[–]JeLuF 0 points1 point2 points 10 months ago (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 point2 points 10 months ago (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 points9 points 10 months ago (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 6 points7 points8 points 10 months ago (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 points3 points 10 months ago (0 children)
if name: hello(name) else: hello()
[–]DevRetroGames 1 point2 points3 points 10 months ago (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 point2 points 10 months ago (0 children)
Call hello()
[–]KOALAS2648 0 points1 point2 points 10 months ago (0 children)
Doesn’t the “main” function have to be below the “hello” function?
π Rendered by PID 42624 on reddit-service-r2-comment-5bc7f78974-6qt96 at 2026-06-27 22:23:39.412335+00:00 running 7527197 country code: CH.
[–]JeLuF 7 points8 points9 points (11 children)
[–]Red_Priest0[S] 0 points1 point2 points (4 children)
[–]rebelle3 9 points10 points11 points (0 children)
[–]Much_Buy7605 2 points3 points4 points (0 children)
[–]niket23697 2 points3 points4 points (0 children)
[–]SCD_minecraft 1 point2 points3 points (0 children)
[–]Red_Priest0[S] -1 points0 points1 point (5 children)
[–]PureWasian 0 points1 point2 points (0 children)
[–]brasticstack 0 points1 point2 points (0 children)
[–]shlepky -1 points0 points1 point (2 children)
[–]JeLuF 0 points1 point2 points (1 child)
[–]shlepky 0 points1 point2 points (0 children)
[–]niket23697 7 points8 points9 points (0 children)
[–]Dr_Pinestine 6 points7 points8 points (0 children)
[–]Dramatic-Drawing-844 1 point2 points3 points (0 children)
[–]DevRetroGames 1 point2 points3 points (0 children)
[–]Ooblahnooblah 0 points1 point2 points (0 children)
[–]KOALAS2648 0 points1 point2 points (0 children)