Check if input is empty

Staying as true to what you provided as I can, I end up with this.

from math import sin, pi


def I1(n, a, b):
    if n is None and a is None and b is None:
        h = 1000
        a = 0
        b = pi
        n =  # ?
    else:
        h = (b - a) / n
    summe = 0
    if a < b:
        for k in range(0, n - 1):
            summe += sin(a + (k + 0.5) * h)
            summe *= pi / n
        print("Das Integral von", a, "bis", b, "entspricht ungefähr:", summe)
    else:
        print("Beachte, die untere Grenze 'a' sollte kleiner als die obere",
            "Grenze 'b'")

That’s assuming you want to set the defaults if all of the arguments are None. I’m not sure that condition is what you’re looking for, however. If I had to guess your intent, I would replace the condition with something like this.

if n is None:
    n = 1000
if a is None:
    a = 0
if b is None:
    b = pi
h = (b - a) / n

Or if you want to make sure they’re numbers and not just not None, you could do something like this.

if not isinstance(n, int) and not isinstance(n, float):
    n = 1000
if not isinstance(a, int) and not isinstance(a, float):
    a = 0
if not isinstance(b, int) and not isinstance(b, float):
    b = pi
h = (b - a) / n

Leave a Comment