fork download
  1. import random
  2. import sys
  3.  
  4. # Function to determine winner
  5. # Returns True if player wins, False if computer wins, None if its a draw
  6. def judge(player, computer):
  7. if player == computer: return None
  8. player_wins = [(0,1), (1,2), (2,0)]
  9. return (player, computer) in player_wins
  10.  
  11. # Simply prints the results
  12. def show_results(player, computer, result):
  13. rps_map = ['rock', 'scissors', 'paper']
  14. res_map = {True: 'Win', False: 'Lost', None: 'Draw'}
  15. print("Player is {}. Computer is {}. You {}!".format(
  16. rps_map[player],
  17. rps_map[computer],
  18. res_map[result])
  19. )
  20.  
  21. def play_round():
  22. try:
  23. player = int(input("Enter your choice in number (rock 1 / paper 2 / scissors 3): ")) - 1
  24. except:
  25. print("Invalid Input Quitting...")
  26. sys.exit(0)
  27.  
  28. computer = random.randint(0,2)
  29.  
  30. result = judge(player, computer)
  31. show_results(player, computer, result)
  32. return result
  33.  
  34.  
  35. def main():
  36. gameOver = False
  37. playerScore = 0
  38. computerScore = 0
  39.  
  40. while not gameOver:
  41. player_wins = play_round()
  42. if player_wins == True:
  43. playerScore += 1
  44. computerScore = 0
  45. if player_wins == False:
  46. playerScore = 0
  47. computerScore += 1
  48. if player_wins == None:
  49. # Draw, do nothing to the scores
  50. pass
  51. if playerScore == 2 or computerScore == 2:
  52. print("Game over")
  53. print(" playerScore:", playerScore)
  54. print(" computerScore:", computerScore)
  55. gameOver = True
  56.  
  57. if __name__ == "__main__": main()# your code goes here
Success #stdin #stdout 0.02s 10088KB
stdin
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
stdout
Enter your choice in number (rock 1 / paper 2 / scissors 3): Player is rock.  Computer is rock.  You Draw!
Enter your choice in number (rock 1 / paper 2 / scissors 3): Player is rock.  Computer is paper.  You Lost!
Enter your choice in number (rock 1 / paper 2 / scissors 3): Player is rock.  Computer is paper.  You Lost!
Game over
('  playerScore:', 0)
('  computerScore:', 2)