/
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. 18.10.2022 at 09:36 am / The Airplane Test of Fluency
  2. 11.02.2021 at 01:13 am / 46 Simple Python Exercises/#02
  3. 05.03.2020 at 01:45 am / The Artificers' Duty of Care
  4. 19.02.2020 at 10:25 am / STEM, STEAM and Art
  5. 14.12.2018 at 03:09 pm / Recursion, Succinctly Put
  6. 15.12.2017 at 12:00 am / The Python Paradox
  7. 11.12.2013 at 12:00 am / Lighting Wizardry
  8. 21.07.2012 at 12:00 am / Hunted By GLaDoS
  9. 28.08.2010 at 12:00 am / 身から出た錆
  10. 15.08.2010 at 12:00 am / 海老
© Wan Zafran. See disclaimer.