Advertisement
Guest User

Untitled

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