Guest User

Untitled

a guest
May 21st, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.27 KB | None | 0 0
  1. #import libraries
  2. import math
  3. import random
  4.  
  5.  
  6. # 0 - rock
  7. # 1 - Spock
  8. # 2 - paper
  9. # 3 - lizard
  10. # 4 - scissors
  11.  
  12. # helper functions
  13.  
  14. #will associate a number between 0 and 4 with a given name in RPSLS
  15. def name_to_number(name):
  16. if (name == "Rock") or (name == "rock"):
  17. return 0
  18. elif (name == "Spock") or (name == "spock"):
  19. return 1
  20. elif (name == "Paper") or (name == "paper"):
  21. return 2
  22. elif (name == "Lizard") or (name == "lizard"):
  23. return 3
  24. elif (name == "Scissors") or (name == "scissors"):
  25. return 4
  26. else:
  27. print "That's from a different game dude"
  28.  
  29.  
  30. #will convert numbers back to the name for output
  31. def number_to_name(number):
  32. if number == 0:
  33. return "Rock"
  34. elif number == 1:
  35. return "Spock"
  36. elif number == 2:
  37. return "Paper"
  38. elif number == 3:
  39. return "Lizard"
  40. elif number == 4:
  41. return "Scissors"
  42.  
  43.  
  44. #main game
  45. def rpsls(player_choice):
  46.  
  47.  
  48. # print a blank line to separate consecutive games
  49. print "\n"
  50.  
  51. # print out the message for the player's choice
  52. print "Player chooses " + player_choice
  53.  
  54. # convert the player's choice to player_number using the function name_to_number()
  55. player_number = name_to_number(player_choice)
  56.  
  57. # compute random guess for comp_number using random.randrange()
  58. comp_number = random.randrange(0,5)
  59.  
  60. # convert comp_number to comp_choice using the function number_to_name()
  61. comp_choice = number_to_name(comp_number)
  62.  
  63. # print out the message for computer's choice
  64. print "Computer chooses " + comp_choice
  65.  
  66. # compute difference of comp_number and player_number modulo five
  67. result = (comp_number - player_number) % 5
  68.  
  69. # use if/elif/else to determine winner, print winner message
  70. if result == 0:
  71. print "Player and computer tie!"
  72. elif result == 1:
  73. print "Computer wins!"
  74. elif result == 2:
  75. print "Computer wins!"
  76. elif result == 3:
  77. print "You win!"
  78. elif result == 4:
  79. print "You win!"
  80.  
  81.  
  82. # test your code - THESE CALLS MUST BE PRESENT IN YOUR SUBMITTED CODE
  83. rpsls("spock")
  84. rpsls("Spock")
  85. rpsls("Paper")
  86. rpsls("lizard")
  87. rpsls("scissors")
  88.  
  89.  
  90. # always remember to check your completed program against the grading rubric
Add Comment
Please, Sign In to add comment