Advertisement
Guest User

Untitled

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