Advertisement
brendan-stanford

RPS-extended

Jan 1st, 2020
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.58 KB | None | 0 0
  1. from random import choice
  2.  
  3. plays = ["Rock", "Paper", "Scissors"]
  4.  
  5. #outcome function
  6. def outcome(winner, pscore, cscore):
  7. print("\n")
  8. if winner == "Player":
  9. print(winner + " score!")
  10. pscore += 1
  11. return pscore
  12. elif winner == "Computer":
  13. print(winner + " score!")
  14. cscore +=1
  15. return cscore
  16. else:
  17. print("Draw!")
  18. print("\n")
  19.  
  20. #main game function
  21. def rock_paper_scissors():
  22. #set up player scores and loop exit condition
  23. quit = False
  24. p_score = 0
  25. c_score = 0
  26. while quit == False:
  27. #try running game code; if inout is incorrect format, remind user
  28. try:
  29. #collect user choice input and randomly choose computer move using choice mthod from random library
  30. user_choice = int(input("Type the number for your choice of action: Rock[0], Paper[1], Scissors[2] or Quit[other]?"))
  31. computer_play = choice(plays)
  32.  
  33. #If the user chose a game move by typing 0-2, print their corresponding choice and the computer's too
  34. print("\n")
  35. if user_choice >= 0 and user_choice <= 2:
  36. user_play = plays[user_choice]
  37. print("Player chooses: " + user_play)
  38. print("Computer chooses: " + computer_play)
  39. #if the user chose 3 they wish to end the game; make user_play Quit so no conditions match
  40. else:
  41. print("Player chooses: Quit")
  42. quit = True
  43. user_play = "Quit"
  44.  
  45. #Draw conditions
  46. if computer_play == user_play:
  47. outcome("draw", p_score, c_score)
  48.  
  49. #Rock conditions
  50. elif user_play == "Rock" and computer_play == "Scissors":
  51. p_score = outcome("Player", p_score, c_score)
  52.  
  53. #Paper conditions
  54. elif user_play == "Paper" and computer_play == "Rock":
  55. p_score = outcome("Player", p_score, c_score)
  56.  
  57. #Scissors conditions
  58. elif user_play == "Scissors" and computer_play == "paper":
  59. p_score = outcome("Player", p_score, c_score)
  60.  
  61. #quit condition
  62. elif user_play == "Quit":
  63. print("Thanks for playing!")
  64. quit = True
  65.  
  66. #Computer win condition
  67. else:
  68. c_score = outcome("Computer", p_score, c_score)
  69.  
  70. #print new line and return player and computer scores
  71. print("\n")
  72. print("Player score: " + str(p_score), "Computer score: " + str(c_score))
  73. print("\n")
  74.  
  75. #if input is not an integer, remind user
  76. except ValueError:
  77. print("\n")
  78. print("Incorrect input format; input must be an integer!")
  79. print("\n")
  80.  
  81. #calling game function
  82. rock_paper_scissors()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement