Things of interest.

 
 
February 11, 2021

46 Simple Python Exercises/#01


Define a function max() that takes two numbers as arguments and returns the largest of them.

The problem:

Define a function max() that takes two numbers as arguments and returns the largest of them.

Use the if-then-else construct available in Python. (It is true that Python has the max() function built in, but writing it yourself is nevertheless a good exercise.)

Solution:

    def new_max(a, b):

    assert b != a, "Values are the same."
    if a > b:
        return a
    elif b > a:
        return b

    print(new_max(46, 46.5))

 

- Areas: Code / Music / Gaming / Languages
- Lang: Japanese / French / English
- Hume: Law / Work / Learning / People
- All: Tags / Posts / Search
 
© 1997 - 2021 / Info