commit e52972b461b7f362c67903d9884c21d6cf299565 Author: Antostarwars Date: Thu May 14 08:55:40 2026 +0200 aggiunto percettrone da hub scuola diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..f48ea0f --- /dev/null +++ b/.gitignore @@ -0,0 +1,46 @@ +# Python bytecode and caches +__pycache__/ +*.py[cod] +*$py.class + +# Virtual environments +.venv/ +venv/ +env/ +ENV/ + +# Packaging and build output +build/ +dist/ +*.egg-info/ +.eggs/ + +# Test, coverage, and type-checker caches +.pytest_cache/ +.coverage +htmlcov/ +.mypy_cache/ +.ruff_cache/ +.pyre/ + +# Jupyter notebooks +.ipynb_checkpoints/ + +# IDE and editor files +.idea/ +.vscode/ +*.swp +*.swo +*~ + +# OS files +.DS_Store +Thumbs.db + +# Local environment files +.env +.env.* + +# Codex/agent workspace metadata +.agents/ +.codex/ diff --git a/main.py b/main.py new file mode 100644 index 0000000..5d53ece --- /dev/null +++ b/main.py @@ -0,0 +1,39 @@ +import sys +import percettrone + +def main(): + successo, weights, bias = percettrone.carica_pesi("pesi_concerto.txt") + + if not successo: + print("Terminazione del programma a causa di un errore nel caricamento.") + sys.exit(1) + + print("Inserisci i dati:") + input_data = [] + + domande = [ + "Artista famoso? (1=Si, 0=No): ", + "Bel meteo? (1=Si, 0=No): ", + "Amici presenti? (1=Si, 0=No): ", + "Cibo buono? (1=Si, 0=No): ", + "Alcool disponibile? (1=Si, 0=No): " + ] + + for domanda in domande: + while True: + try: + risposta = int(input(domanda)) + input_data.append(risposta) + break + except ValueError: + print("Errore: Inserisci solo numeri interi (1 o 0).") + + decisione = percettrone.prevedi(weights, bias, input_data) + + if decisione == 1: + print("\nVai al concerto!") + else: + print("\nResta a casa!") + +if __name__ == "__main__": + main() \ No newline at end of file diff --git a/percettrone.py b/percettrone.py new file mode 100644 index 0000000..e3534e7 --- /dev/null +++ b/percettrone.py @@ -0,0 +1,54 @@ +FEATURES = 5 +THRESHOLD = 0.5 + +def activation(x: float) -> int: + if x > THRESHOLD: + return 1 + else: + return 0 + +def carica_pesi(filename: str): + weights = [] + + try: + with open(filename, "r") as file: + for i in range(FEATURES): + line = file.readline().strip() + if line.startswith("Peso"): + try: + val = float(line.split(':')[1].strip()) + weights.append(val) + except (IndexError, ValueError): + print(f"Errore nella lettura del peso {i}") + return False, None, None + else: + print(f"Errore nella lettura del peso {i}") + return False, None, None + + line = file.readline().strip() + if line.startswith("Bias:"): + try: + bias = float(line.split(':')[1].strip()) + except (IndexError, ValueError): + print("Errore nella lettura del bias") + return False, None, None + else: + print("Errore nella lettura del bias") + return False, None, None + + return True, weights, bias + + except FileNotFoundError: + print(f"Errore: file {filename} non trovato!") + return False, None, None + except Exception as e: + print(f"Errore imprevisto: {e}") + return False, None, None + +def prevedi(weights: list, bias: float, input_data: list) -> int: + if len(weights) != FEATURES or len(input_data) != FEATURES: + raise ValueError(f"Attesi {FEATURES} pesi e {FEATURES} input.") + + somma = bias + sum(w * i for w, i in zip(weights, input_data)) + + return activation(somma) \ No newline at end of file diff --git a/pesi_concerto.txt b/pesi_concerto.txt new file mode 100644 index 0000000..7b6a537 --- /dev/null +++ b/pesi_concerto.txt @@ -0,0 +1,6 @@ +Peso 1: 0.7 +Peso 2: 0.6 +Peso 3: 0.5 +Peso 4: 0.3 +Peso 5: 0.4 +Bias: 0.0 \ No newline at end of file