Advertisement
Guest User

Untitled

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