LU10.L03 - Einleser erweitern

Aufgaben

Teilaufgabe 1

Nehmen Sie die Github-Classroom Aufgabe an und clonen Sie das Repository in ihre Entwicklungsumgebung.

Studieren Sie die zwei Python Files input_reader.py und main.py.

Teilauftrag 2

None

def read_float(text_to_display, lower_bound = None, upper_bound = None):
...
 
def read_int(text_to_display, lower_bound = None, upper_bound = None):
...

math.inf

def read_float(text_to_display, lower_bound = float(-inf), upper_bound = float(inf)):
...
 
def read_int(text_to_display, lower_bound = int(-inf), upper_bound = int(inf)):
...

Teilauftrag 3

Überlegen Sie sich, wie Sie die Funktion read_float(text_to_display, lower_bound = XX, upper_bound = XX): anpassen müssen, damit die Überprüfung auf die Grenzwerte funktioniert.

None

def read_float(text_to_display, lower_bound = None, upper_bound = None):
    '''
    Read a float from the user within bounds
    '''
    while True:
        try:
            num = float(input(text_to_display))
        except ValueError:
            print("Please, enter a real number!")
            continue
        else:
            if lower_bound is not None and num < lower_bound:
                print("Please, enter a number greater than or equal to", lower_bound)
                continue
            if upper_bound is not None and num > upper_bound:
                print("Please, enter a number less than or equal to", upper_bound)
                continue
            return num
 
def read_int(text_to_display, lower_bound = None, upper_bound = None):
    '''
    Read an int from the user within bounds
    '''
    while True:
        try:
            num = int(input(text_to_display))
        except ValueError:
            print("Please, enter a whole number!")
            continue
        else:
            if lower_bound is not None and num < lower_bound:
                print("Please, enter a number greater than or equal to", lower_bound)
                continue
            if upper_bound is not None and num > upper_bound:
                print("Please, enter a number less than or equal to", upper_bound)
                continue
            return num

math.inf

from math import inf
 
def read_float(text_to_display, lower_bound = float(-inf), upper_bound = float(inf)):
    '''
    Read a float from the user within bounds
    '''
    while True:
        try:
            num = float(input(text_to_display))
        except ValueError:
            print("Please, enter a real number!")
            continue
        else:
            if num < lower_bound:
                print("Please, enter a number greater than or equal to", lower_bound)
                continue
            if num > upper_bound:
                print("Please, enter a number less than or equal to", upper_bound)
                continue
            return num
 
 
def read_int(text_to_display, lower_bound = float(-inf), upper_bound = float(inf)):
    '''
    Read an int from the user within bounds
    '''
    while True:
        try:
            num = int(input(text_to_display))
        except ValueError:
            print("Please, enter a whole number!")
            continue
        else:
            if num < lower_bound:
                print("Please, enter a number greater than or equal to", lower_bound)
                continue
            if num > upper_bound:
                print("Please, enter a number less than or equal to", upper_bound)
                continue
            return num