Sclafus

morra cinese

Apr 17th, 2020
249
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.65 KB | None | 0 0
  1. import random
  2. import sys
  3. import time
  4. options = ['sasso', 'carta', 'forbici']
  5.  
  6.  
  7. def check(a, b):
  8.     '''a=user option, b=npc option, returns True if user won, else False'''
  9.     if a == 'sasso' and b == 'carta':
  10.         return False
  11.     elif a == 'carta' and b == 'forbici':
  12.         return False
  13.     elif a == 'forbici' and b == 'sasso':
  14.         return False
  15.     elif a == b:
  16.         return -1
  17.     else:
  18.         return True
  19.  
  20.  
  21. def valid_input_checker(a):
  22.     '''Checks if the input is sasso, carta or forbice,
  23.    else it loops until it is one of them, returns True if valid'''
  24.     while True:
  25.  
  26.         if (a == 'sasso' or a == 'forbici' or a == 'carta'):
  27.             return True
  28.         a = input("Riprova. Sasso, carta o forbici?")
  29.  
  30.  
  31. def main():
  32.     flag_play = input("Vuoi giocare a morra cinese?(y/n)")
  33.     if flag_play == 'y':
  34.         flag_play = True
  35.     else:
  36.         flag_play = False
  37.  
  38.     while (flag_play):
  39.         cpu_choice = random.choice(options)
  40.         user_choice = input("sasso, carta o forbici?")
  41.         user_choice = user_choice.lower()
  42.         if valid_input_checker(user_choice):
  43.             if check(user_choice, cpu_choice) == True:
  44.                 print(
  45.                     "Hai vinto! Il computer ha giocato {0}. Congratulazioni!".format(cpu_choice))
  46.             elif check(user_choice, cpu_choice) == False:
  47.                 print(
  48.                     "Hai perso! Il computer ha giocato {0}. Mi spiace".format(cpu_choice))
  49.             else:
  50.                 print(
  51.                     "Pareggio! Avete entrambi giocato {0}".format(cpu_choice))
  52.         flag_play = input("Vuoi giocare ancora?(y/n)")
  53.         if flag_play == 'y':
  54.             flag_play = True
  55.  
  56.  
  57. main()
Advertisement
Add Comment
Please, Sign In to add comment