How to Create Rock Paper Scissor Game in Python

In this tutorial, we will program 'How to Create Rock Paper Scissors Game in Python'. This time, we will learn to code a game called Rock, Paper, Scissors. The objective is to provide a proper insight for creating a simple game in Python programming. I will provide a sample program to demonstrate the actual coding of this tutorial.

This tutorial will demonstrate the proper way of creating a game using a specific library. The program I will show you is the creation of a simple game called Rock, Paper, Scissors. I will do my best to provide you with the best way to retrieve the CSV file. So, let's start with the coding.

Getting Started:

First you will have to download & install the Python IDLE's, here's the link for the Integrated Development And Learning Environment for Python https://www.python.org/downloads/.

Creating Main Function

This is the main function of the application. The following code will display a simple GUI in terminal console that will display program. To do this, simply copy and paste these blocks of code into the IDLE text editor.
  1. import random
  2. import os
  3. import re
  4.  
  5.  
  6. def check_play_status():
  7. option = ['yes', 'no']
  8. while True:
  9. try:
  10. response = input('Do you wish to play again? (Yes or No): ')
  11. if response.lower() not in option:
  12. raise ValueError('Yes or No only')
  13.  
  14. if response.lower() == 'yes':
  15. return True
  16. else:
  17. os.system('cls' if os.name == 'nt' else 'clear')
  18. print('Thanks for playing!')
  19. exit()
  20.  
  21. except ValueError as err:
  22. print(err)
  23.  
  24.  
  25. def play_rps():
  26. play = True
  27. while play:
  28. os.system('cls' if os.name == 'nt' else 'clear')
  29. print('')
  30. print('================ RPS Game ================\n')
  31.  
  32. user_choice = input('Choose your weapon'
  33. ' [R]ock], [P]aper, or [S]cissors: ')
  34.  
  35. if not re.match("[SsRrPp]", user_choice):
  36. print('Please choose a letter:')
  37. print('[R]ock, [P]aper, or [S]cissors')
  38. continue
  39.  
  40. print(f'You chose: {user_choice}')
  41.  
  42. choices = ['R', 'P', 'S']
  43. opp_choice = random.choice(choices)
  44.  
  45. print(f'I chose: {opp_choice}')
  46.  
  47. if opp_choice == user_choice.upper():
  48. print('Tie!')
  49. play = check_play_status()
  50. elif opp_choice == 'R' and user_choice.upper() == 'S':
  51. print('Rock beats scissors, I win!')
  52. play = check_play_status()
  53. elif opp_choice == 'S' and user_choice.upper() == 'P':
  54. print('Scissors beats paper! I win!')
  55. play = check_play_status()
  56. elif opp_choice == 'P' and user_choice.upper() == 'R':
  57. print('Paper beats rock, I win!')
  58. play = check_play_status()
  59. else:
  60. print('You win!\n')
  61. play = check_play_status()
  62.  
  63.  
  64. if __name__ == '__main__':
  65. play_rps()

This Python script implements a Rock-Paper-Scissors game. It allows the user to play against the computer. The game continues until the user decides to stop playing.

After each round, the script asks the user if they want to play again. If the user inputs invalid options, the script prompts them to choose again. If the user chooses to stop playing, the script prints a thank you message and exits.

Output:

The How to Create Rock Paper Scissor Game in Python source code that I provide can be download below. Please kindly click the download button.

There you have it we successfully created How to Create Rock Paper Scissor Game in Python. I hope that this simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!

More Tutorials for Python Language

Python Tutorials

Add new comment