Python

Asked 2 years ago, Updated 2 years ago, 13 views


def f(a,b,c):
    if a>b and a>c:
        max=a
    elif b>a and b>c:
        max=b
    else:
        max=c


    if a<b and a<c:
        min=a
    elif b<a and b<c:
        min=b
    else:
        min=c
    return max, min

print(f(5,3,4))

Whatever integer you put in, whatever you put in your face, you put in your face, and you put in your face a function that gives you the largest and smallest numbers I have to make it, so do I do it like the top?

Also, when you do print(f(5,3,4), parentheses come out like this Why is there a parenthesis? Is there any way to make it come out?

python

2022-09-21 20:15

2 Answers

Yes. You can do as you wrote. Logically, there's no problem, right? This will work normally. But I think there are some unnecessary things.

def f(a,b,c):
    if a>b and a>c:
        max=a
    #elif b>a and b>c:
    elif b>c:
        max=b
    We know that the value of a is not the largest, so we only need to compare b and c.
    else:
        max=c
    #
    #Up to the top line was to get the max value.
    #
    if a<b and a<c:
        min=a
    #elif b<a and b<c:
    elif b<c:
        min=b
        #The same goes for this. We know that a is not a minimum, so we only need to compare b and c.
    else:
        min=c
    return max, min

print(f(5,3,4))

When viewing C language, can return only one object as a return value. If you write return 0,1; in C language, you will get 100% compilation error. That's not much different for Python. It's because our smart Python automatically catches on and returns the two together . Only one object can be returned as a return value. If Python tries to return multiple, it's tied up in a tuple and returned as if it were one ^ ^ 0 ^

..


2022-09-21 20:15

If you're not practicing algorithms... You can use the max and min functions.

def f(a, b, c):
    return max([a, b, c]), min([a, b, c])

In the case of a return, if it is more than two, it will be received as a triple, so you get ().

val1, val2 = f(5, 3, 4)

If you receive the number of variables according to the number, it will not be returned to the tuple.


2022-09-21 20:15

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.