Function arguments | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Function arguments

I want to make a function that prints hello world if no argument is provided or the argument if it is provided https://code.sololearn.com/cHtyvT4ky0rt/?ref=app Help.

12th Jun 2020, 10:58 AM
Tomiwa Joseph
Tomiwa Joseph - avatar
7 Answers
+ 4
Like you provided the default value for parameter option 'Hello world!' Give parameter "a" a default value of None. That way if you enter greet(), when your code checks parameter "a", it sees it is none and prints Hello World! https://code.sololearn.com/cHPh0qK8fep3/?ref=app
12th Jun 2020, 11:22 AM
Slick
Slick - avatar
+ 4
Your code works fine. The error being generated is caused by your def(), which is missing the arguments you defined during the set up
12th Jun 2020, 11:10 AM
Rik Wittkopp
Rik Wittkopp - avatar
+ 3
I am thinking you may need to create an if / else scenario which will determine which of 2 def options to run depending upon the input. Nice concept to play with
12th Jun 2020, 11:13 AM
Rik Wittkopp
Rik Wittkopp - avatar
+ 3
Slick Nice!
12th Jun 2020, 11:23 AM
Rik Wittkopp
Rik Wittkopp - avatar
+ 3
Thanks guys.
12th Jun 2020, 11:33 AM
Tomiwa Joseph
Tomiwa Joseph - avatar
+ 2
But...damn! That was Slick๐Ÿ˜„๐Ÿ˜ƒ๐Ÿ˜ƒ
12th Jun 2020, 11:34 AM
Tomiwa Joseph
Tomiwa Joseph - avatar
+ 2
By the way, I think a cleaner way to set it up would be this: def greet(a=None): print(a or 'Hello World')
12th Jun 2020, 7:31 PM
HonFu
HonFu - avatar