/
11.02.2021 at 12:56 am
Cuttings

46 Simple Python Exercises/#01

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

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))

Filed under:
#
#
Words: 98 words approx.
Time to read: 0.39 mins (at 250 wpm)
Keywords:
, , , , , , , , ,

Potentially related: 46 Simple Python Exercises/#02

Other suggested posts

  1. 02.01.2024 at 08:16 pm / Go Interfaces - By Witcher-Like Metaphors
  2. 03.07.2022 at 05:15 pm / The Unpreparedness Apology
  3. 20.08.2020 at 08:59 pm / A Case for Legalese (Or Not)
  4. 05.03.2020 at 01:45 am / The Artificers' Duty of Care
  5. 11.01.2019 at 06:39 pm / Clarity of Liskov
  6. 08.02.2016 at 12:00 am / Eye to Eye (Jonathan Young's Version)
  7. 22.07.2015 at 12:00 am / Fair Judges of Fair Play
  8. 29.11.2013 at 12:00 am / ガラケー Garakei Evolution
  9. 25.09.2010 at 12:00 am / 汗牛充棟
  10. 16.08.2010 at 12:00 am / Lost Nuances in 'Ivan the Terrible'
© Wan Zafran. See disclaimer.