#๐Ÿ”’ Can I make optional params in my functions?

19 messages ยท Page 1 of 1 (latest)

hard vortex
#

I have a very basic function here, and I was wondering if the prompt could be made optional? for instance I would like to be able to have the flexibility of using this function both with and without the parameters I have set.

wicked elmBOT
#

@hard vortex

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

hard vortex
#

Can I make optional params in my functions?

weak hound
#

Have you tried searching for optional parameters? This is a very common topic.

hard vortex
#

well I've looked at optional parameters. But the only thing that comes up is using a replacement when no parameter is provided. I want it to run as if there is no parameter if none is provided.

#

If possible

split shale
#

try 'prompt=None'
if prompt is passed then u get whatever is passed through it. otherwise it will be None. think of it as like a default value of the parameter

weak hound
#

I'm not sure what you had found during your search, but it's simply:

def get_string(prompt="Some default value"):
weak hound
hard vortex
#

Yea i just tried it I can do it with just a blank string it seems

weak hound
#

Yes, a empty string is a valid default value.

hard vortex
#

Should that be a fine fix or is there a better alternative?

weak hound
hard vortex
#

Alright, thank you!

weak hound
#

You're welcome.

#

You can !close if you're done with this thread.

hard vortex
#

!close

wicked elmBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.