/
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. 04.07.2024 at 08:43 am / Chronic Key Switching
  2. 11.06.2022 at 11:30 am / Salted Correspondences
  3. 16.12.2017 at 12:00 am / Sketch, Draft, Craft
  4. 24.01.2016 at 12:00 am / Prayers Like Magic
  5. 19.07.2015 at 12:00 am / Deadened Demonyms
  6. 20.06.2015 at 12:00 am / Mike Tyson on (His) Lawyers
  7. 15.06.2015 at 12:00 am / Supposedly Cultivated Tastes
  8. 14.06.2015 at 12:00 am / Just the Two of Us (6-Bar Guitar/Sax Copy)
  9. 02.12.2013 at 12:00 am / Coloured Plasmids
© Wan Zafran. See disclaimer.