/
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.
Table of Contents

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. 29.01.2020 at 10:48 am / Code & Shame
  2. 23.08.2018 at 09:53 am / Nostalgic Non-Memories
  3. 12.07.2016 at 12:00 am / Refunctin' Blocks
  4. 12.03.2016 at 12:00 am / Transcript of Tifa Funk - Interlude Solo (Tifa's Theme by Tetrimino)
  5. 15.06.2015 at 12:00 am / Supposedly Cultivated Tastes
  6. 14.06.2015 at 12:00 am / Just the Two of Us (6-Bar Guitar/Sax Copy)
  7. 03.01.2014 at 12:00 am / Write With Vigour
  8. 26.12.2013 at 12:00 am / Teal Stares
  9. 14.04.2012 at 12:00 am / Ghostly Portal
  10. 16.03.2012 at 12:00 am / Sentient Vocal Directives
© Wan Zafran. See disclaimer.