gruntfutuk

penalty shootout

Sep 26th, 2020
847
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. from random import choice
  2. from itertools import cycle
  3.  
  4. def get_user_choice(prompt="Enter option: ", options=None):
  5.     if options is None:
  6.         options = directions
  7.     while True:
  8.         option = input(prompt).strip().lower()
  9.         if option in options:
  10.             return option
  11.         print('That is not a valid choice.')
  12.         print(f'Options are: {", ".join(option.title() for option in options)}')
  13.  
  14. def report(action, user_option, computer_option, saved):
  15.  
  16.     def reporting(team1, move1, team2, move2):
  17.         report_txt = f"\n{team1} team shoots to the {move1}\n" \
  18.                 f"{team2} team goalkeeper dives to the {move2}\n"
  19.         if saved:
  20.             report_txt += "SAVED!!!\n"
  21.         else:
  22.             report_txt += f"GOAL !!! for {team1} team\n"
  23.         return report_txt
  24.  
  25.     if action == "Shoot":
  26.         return reporting("User", user_option, "Computer", computer_option)
  27.     else:
  28.         return reporting("Computer", computer_option, "User", user_option)
  29.      
  30. def take_penalty_each():
  31.     user_goals = 0
  32.     computer_goals = 0
  33.     for _ in range(2): # loops twice, once for each team
  34.         user_action = next(action)
  35.         user_option = get_user_choice(action_prompt[user_action])
  36.         computer_option = choice(directions)
  37.         saved = (user_option, computer_option) in saves
  38.         outcome = report(user_action, user_option, computer_option, saved)
  39.         if not saved:
  40.             if user_action == "Shoot":
  41.                 user_goals += 1
  42.             else:
  43.                 computer_goals +=1
  44.         print(outcome)
  45.     return user_goals, computer_goals
  46.        
  47. def print_scores():
  48.     final = total_penalties == max_penalties
  49.     if final:
  50.         print('\n\nFINAL SCORES\n')
  51.     else:
  52.         print('\n\nSCORES\n')
  53.     print(f"The score is now USER: {user_team_score} "
  54.           f"COMPUTER: {computer_team_score}")
  55.     if final:
  56.         if user_team_score > computer_team_score:
  57.             print("Well done you won")
  58.         elif user_team_score == computer_team_score:
  59.             print("A draw")
  60.         else:
  61.             print("You Lose")
  62.     print()
  63.  
  64.  
  65. directions = ["left", "middle", "right"]
  66. saves = (("right", "left"), ("middle", "middle"), ("left", "right"))
  67. action = cycle(['Shoot', 'Keep'])
  68. action_prompt = {'Shoot': "Pick your spot: ",
  69.                  'Keep': "Choose dive direction: " }
  70.  
  71. total_penalties = 0
  72. user_team_score = 0
  73. computer_team_score = 0
  74. max_penalties = 10
  75.  
  76. print("------WELCOME TO THE PYTHON PENALTY SHOOTOUT------")
  77. print("The options you can choose are left,right or middle")
  78.  
  79. while total_penalties < max_penalties:
  80.     user_goals, computer_goals = take_penalty_each()
  81.     user_team_score += user_goals
  82.     computer_team_score += computer_goals
  83.     total_penalties += 2
  84.     print_scores()
RAW Paste Data