0
Can anyone tell me how to assign a variable to a function?
hello() def hello(): print("Hello world!") In this case how to assign a variable to hello() function
4 ответов
+ 1
you want to assign the function to a variable and then invoke the function through the variable. right?
If so, after assignment, invoke function using fn(). if not, pls elaborate your problem
+ 1
I'm not quite sure of what you meant exactly, but here's my thoughts.
If you simply want to create a variable that contains the function you can for example:
phello = hello()
print (phello)
However, if you meant to ask how to make a function that takes arguments:
def hello (response=0):
list = ["Hello World", "Hai there", "Greetings"]
try:
print(list[response])
except IndexError:
print("List doesn't contain that response!")
If no arguments are given:
hello ()
# Same as hello (response=0)
# Prints "Hello World"
But with arguments, prints the specified line
0
fn=hello
- 1
can u pl elaborate...it's not working..