Advertisement
Guest User

Untitled

a guest
Sep 22nd, 2019
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.77 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. if user_choice == "rock" or user_choice == "paper" or user_choice == "scissors":
  37. return user_choice
  38. else:
  39. return "Sorry, you entered unknown command."
  40.  
  41.  
  42. def determine_winner(name: str, user_choice: str, computer_choice: str, reverse_name: bool = False) -> str:
  43. """
  44. Determine the winner returns a string that has information about who won.
  45.  
  46. You should use the functions that you wrote before. You should use check_user_choice function
  47. to validate the user choice and use normalize_user_name for representing a correct name. If the
  48. function parameter reverse is true, then you should also reverse the player name.
  49. NB! Use the previous functions that you have written!
  50.  
  51. :param name:player name
  52. :param user_choice:
  53. :param computer_choice:
  54. :param reverse_name:
  55. :return:
  56. """
  57. name = normalize_user_name(name)
  58. name = reverse_user_name(name) True if determine_winner(True) else False
  59. user_choice = check_user_choice(user_choice)
  60. computer_choice = check_user_choice(computer_choice)
  61. if user_choice == computer_choice:
  62. return "Its a Draw!"
  63. elif user_choice == "rock":
  64. if computer_choice == "paper":
  65. return f"{name} had {user_choice} and computer had {computer_choice}, hence computer wins."
  66. if computer_choice == "scissors":
  67. return f"{name} had {user_choice} and computer had {computer_choice}, hence {name} wins."
  68. elif user_choice == "paper":
  69. if computer_choice == "scissors":
  70. return f"{name} had {user_choice} and computer had {computer_choice}, hence computer wins."
  71. if computer_choice == "rock":
  72. return f"{name} had {user_choice} and computer had {computer_choice}, hence {name} wins."
  73. elif user_choice == "scissors":
  74. if computer_choice == "rock":
  75. return f"{name} had {user_choice} and computer had {computer_choice}, hence computer wins."
  76. if computer_choice == "paper":
  77. return f"{name} had {user_choice} and computer had {computer_choice}, hence {name} 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. pass
  93.  
  94.  
  95. if __name__ == "__main__":
  96. print(normalize_user_name('ago')) # Ago
  97. print(normalize_user_name('AGO')) # Ago
  98. print(normalize_user_name('MaRtInA')) # Martina
  99.  
  100. print(reverse_user_name('MaRtInA')) # Anitram
  101. print(reverse_user_name('AGO')) # Oga
  102.  
  103. print(check_user_choice('rock')) # rock
  104. print(check_user_choice('ROCK')) # rock
  105. print(check_user_choice('midagi on viltu')) # Sorry, you entered unknown command.
  106.  
  107. # 50% on juba tehtud, tubli töö!
  108.  
  109. print(determine_winner('ago', 'paper', 'paper')) # Ago had rock and computer had paper, hence computer wins.
  110. print(determine_winner('ago', 'rock', 'paper', True)) # Oga had rock and computer had paper, hence computer wins.
  111. print(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', 'sciSSOrs')) # Shakira had rock and computer had scissors, hence Shakira wins.
  114.  
  115. # play_game() # Kommenteeri see rida välja kui kõik funktsioonid on valmis
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement