Advertisement
Guest User

Untitled

a guest
Sep 16th, 2019
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.64 KB | None | 0 0
  1. """Simple version of rock paper and scissors."""
  2. from random import choice
  3.  
  4. def normalize_user_name(name: str) -> str:
  5. """
  6. Simple function gets player name as input and capitalizes it.
  7.  
  8. :param name: name of the player
  9. :return: A name that is capitalized.
  10. """
  11. c_name = name.capitalize()
  12. return c_name
  13.  
  14. def reverse_user_name(name: str) -> str:
  15. """
  16. Function that takes in name as a parameter and reverses its letters. The name should also be capitalized.
  17.  
  18. :param name: name of the player
  19. :return: A name that is reversed.
  20. """
  21. rev = name[::-1]
  22. c_rev = normalize_user_name(rev)
  23. return c_rev
  24.  
  25.  
  26. def check_user_choice(choice: str) -> str:
  27. """
  28. Function that checks user's choice.
  29.  
  30. The choice can be uppercase or lowercase string, but the choice must be
  31. either rock, paper or scissors. If it is, then return a choice that is lowercase.
  32. Otherwise return 'Sorry, you entered unknown command.'
  33. :param choice: user choice
  34. :return: choice or an error message
  35. """
  36. choice = str(choice).lower()
  37. if choice == "rock":
  38. return "rock"
  39. elif choice == "paper":
  40. return "paper"
  41. elif choice == "scissors":
  42. return "scissors"
  43.  
  44.  
  45.  
  46.  
  47.  
  48.  
  49. def determine_winner(name: str, user_choice: str, computer_choice: str, reverse_name: bool = False) -> str:
  50. """
  51. Determine the winner returns a string that has information about who won.
  52.  
  53. You should use the functions that you wrote before. You should use check_user_choice function
  54. to validate the user choice and use normalize_user_name for representing a correct name. If the
  55. function parameter reverse is true, then you should also reverse the player name.
  56. NB! Use the previous functions that you have written!
  57.  
  58. :param name:player name
  59. :param user_choice:
  60. :param computer_choice:
  61. :param reverse_name:
  62. :return:
  63. """
  64. if reverse_name:
  65. n = reverse_user_name(name)
  66. c = user_choice
  67. n = normalize_user_name(name)
  68. computer_choice = check_user_choice(computer_choice)
  69. if c == computer_choice:
  70. return f"{n} had {c} and computer had {computer_choice}, hence it is a draw."
  71. elif c == "paper":
  72. if computer_choice == "scissors":
  73. return f"{n} had {c} and computer had {computer_choice}, hence computer wins."
  74. else:
  75. return f"{n} had {c} and computer had {computer_choice}, hence {n} wins."
  76. elif c == "rock":
  77. if computer_choice == "paper":
  78. return f"{n} had {c} and computer had {computer_choice}, hence computer wins."
  79. else:
  80. return f"{n} had {c} and computer had {computer_choice}, hence {n} wins."
  81. elif c == "scissors":
  82. if computer_choice == "rock":
  83. return f"{n} had {c} and computer had {computer_choice}, hence computer wins."
  84. else:
  85. return f"{n} had {c} and computer had {computer_choice}, hence {n} wins."
  86. else:
  87. return "There is a problem determining the winner."
  88.  
  89.  
  90.  
  91.  
  92. def play_game() -> None:
  93. """
  94. Enables you to play the game you just created.
  95. :return:
  96. """
  97. user_name = input("What is your name? ")
  98. play_more = True
  99. while play_more:
  100. computer_choice = choice(['rock', 'paper', 'scissors'])
  101. user_choice = check_user_choice(input("What is your choice? "))
  102. print(determine_winner(user_name, user_choice, computer_choice))
  103. play_more = True if input("Do you want to play more ? [Y/N] ").lower() == 'y' else False
  104.  
  105.  
  106. if __name__ == "__main__":
  107. print(normalize_user_name('ago')) # Ago
  108. print(normalize_user_name('AGO')) # Ago
  109. print(normalize_user_name('MaRtInA')) # Martina
  110.  
  111. print(reverse_user_name('MaRtInA')) # Anitram
  112. print(reverse_user_name('AGO')) # Oga
  113.  
  114. print(check_user_choice('rock')) # rock
  115. print(check_user_choice('ROCK')) # rock
  116. print(check_user_choice('midagi on viltu')) # Sorry, you entered unknown command.
  117.  
  118. # 50% on juba tehtud, tubli töö!
  119.  
  120. print(determine_winner('ago', 'rock', 'paper')) # Ago had rock and computer had paper, hence computer wins.
  121. print(determine_winner('ago', 'rock', 'paper', True)) # Oga had rock and computer had paper, hence computer wins.
  122. print(
  123. determine_winner('loORa', 'SCISSORS', 'paper')) # Loora had scissors and computer had paper, hence Loora wins.
  124. print(determine_winner('Shakira', 'waka waka', 'fire')) # There is a problem determining the winner.
  125. print(determine_winner('Shakira', 'rock',
  126. 'sciSSOrs')) # Shakira had rock and computer had scissors, hence Shakira wins.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement