Logo Pastebin.fr
Pastebin

Retrouvez, créez et partagez vos snippets en temps réel.

drtfyguhijokp

Goal: input parsing & type conversion (int/bool), simple validation, f-strings.

Task

Ask the user for: instrument name (string), year of construction (int), and whether it is active (accept true/false, yes/no, 1/0, case-insensitive).
Validate the year in a sensible range (e.g., 1900 to current year).
Normalize the boolean input to a Python bool.
Print a one-line summary like: Instrument: LIDAR-01 | Built: 2018 | Active: True.
Hint: write a helper to_bool(s: str) -> bool | None that returns None on invalid input.^

while True:
    Try :
        name = input('instrument name : ')
        year = int(input('year of construction : '))
        active = input('active ? ')
        active = to_bool(active)
        
        if (year >= 1900 and year <= 2025) :
            year = int(input('year of construction'))
            
        except:
            year = int(input('choose an integer between 1900 and 2025'))
        
def to_bool(active)
            if active == 'yes' or active == '1' :
                active = True
            elif active == 'no' or active == '0' :
                active = False
            return active
    
print('Instrument: ',name,' | Built: ',year,' | Active: ',active)

Créé il y a 3 mois.

Rechercher un Pastebin

Aucun paste trouvé.