/
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. 22.08.2018 at 02:13 pm / Heinrich Schliemann's Learning Method
  2. 15.12.2017 at 12:00 am / The Python Paradox
  3. 03.01.2017 at 12:00 am / Debugging: Twice As Hard As Writing Code
  4. 11.07.2016 at 12:00 am / SublimeREPL's Slow Printing/Freezing - A Solution
  5. 11.08.2015 at 12:00 am / Proper Denials and Bare Denials
  6. 08.12.2013 at 12:00 am / Alien Blue Entrances
  7. 14.04.2012 at 12:00 am / Ghostly Portal
  8. 17.03.2012 at 12:00 am / Upper and Lower Hengsha
  9. 16.03.2012 at 12:00 am / Sentient Vocal Directives
© Wan Zafran. See disclaimer.