开发者

Print the function itself python 3 [closed]

开发者 https://www.devze.com 2023-02-08 14:23 出处:网络
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical andcannot be reasonably answered in its current form. For help clari
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 12 years ago.

Write a program that has a function name开发者_Go百科d my_func with three parameters, a, b, and c.

The first parameter is required, and the second two parameters have the default values of 'b was not entered' and 'c was not entered'.

The function must print the value of each parameter.

In your program, call my_func three times. The first time, just provide a value for the first parameter.

The second time, provide values for the first and second parameters. The third time, provide values for all three parameters.

In your program, print the function itself.

the output should be:

test
b was not entered
c was not entered
test
test
c was not entered
test
test
test
<function my_func at 0x397588>

here is what i got so far:

def my_func(a, b=False, c=False):
    """Prints out the values"""
    for i in a:
        print(i)

    if not b:
        print("b was not entered")
    else:
        for i in b:
            print(i)


    if not c:
        print("C was not entered")
    else:
        for i in c:
            print(i)


my_func(['test'])
my_func(['test'], ['test'])
my_func(['test'], ['test'], ['test'])


I'll try to give you some pointers to find the correct answer yourself.

Firstly, why are you calling your function with list of string as parameter instead of just string. Passing just string would simplify your function.

Secondly, when you specify the default value for your b and c arguments, you can use whatever value you want, including strings. That would simplify your function further.


You seem to be missing only the last part:

In your program, print the function itself.

Just add this:

print(my_func)

and you'll get output exactly like you asked.

Also, while your code now produces the output, you're using False as default value, instead of what was asked. You're also using lists for each parameter, and the question doesn't ask you to do that. You could define your function like this:

def my_func(a, b="b was not entered", c="C was not entered"):
    print(a)
    print(b)
    print(c)

And call it like this:

my_func('test')
my_func('test', 'test')
my_func('test', 'test', 'test')
print(my_func)

That's a lot simpler and produces the same output

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号