import random import sys import time options = ['sasso', 'carta', 'forbici'] def check(a, b): '''a=user option, b=npc option, returns True if user won, else False''' if a == 'sasso' and b == 'carta': return False elif a == 'carta' and b == 'forbici': return False elif a == 'forbici' and b == 'sasso': return False elif a == b: return -1 else: return True def valid_input_checker(a): '''Checks if the input is sasso, carta or forbice, else it loops until it is one of them, returns True if valid''' while True: if (a == 'sasso' or a == 'forbici' or a == 'carta'): return True a = input("Riprova. Sasso, carta o forbici?") def main(): flag_play = input("Vuoi giocare a morra cinese?(y/n)") if flag_play == 'y': flag_play = True else: flag_play = False while (flag_play): cpu_choice = random.choice(options) user_choice = input("sasso, carta o forbici?") user_choice = user_choice.lower() if valid_input_checker(user_choice): if check(user_choice, cpu_choice) == True: print( "Hai vinto! Il computer ha giocato {0}. Congratulazioni!".format(cpu_choice)) elif check(user_choice, cpu_choice) == False: print( "Hai perso! Il computer ha giocato {0}. Mi spiace".format(cpu_choice)) else: print( "Pareggio! Avete entrambi giocato {0}".format(cpu_choice)) flag_play = input("Vuoi giocare ancora?(y/n)") if flag_play == 'y': flag_play = True main()