Hi there !
Today I will share some tips to make a simple stone paper scissor game in python. The game will be a Human vs Computer game.
import random
Why?"
So that computer will generate its own choice.
💡 Place a infinite while loop (You don’t have to run program again and again.)
while True:
# Rules of the game
print("""Enter your choice :
a. Press '1' to select 'Stone'.
b. Press '2' to select 'Paper'.
c. Press '3' to select 'Scissor'.\n""")
user_choice = int(input("Enter YOUR choice: "))
while user_choice > 3 or user_choice < 1:
user_choice = int(input("Enter valid input: "))
if user_choice == 1:
choice_name = 'Stone'
elif user_choice == 2:
choice_name = 'Paper'
else:
choice_name = 'Scissor'
computer_choice = random.randint(1, 3)
# Assigning numbers to the computer's choices
if computer_choice == 1:
computer_choicehoice_name = 'Stone'
elif computer_choice == 2:
computer_choicehoice_name = 'Paper'
else:
computer_choicehoice_name = 'Scissor'
if((user_choice == 1 and computer_choice == 2) or
(user_choice == 2 and computer_choice ==1 )):
print("Paper wins !!! \n", end = "")
result = "Paper"
elif((user_choice == 1 and computer_choice == 3) or
(user_choice == 3 and computer_choice == 1)):
print("Stone wins !!! \n", end = "")
result = "Stone"
else:
print("Scissor wins !!!\n", end = "")
result = "Scissor"
if result == choice_name:
print("\nYOU WIN !!!\n")
else:
print("\nCOMPUTER WINS !!!\n")
print("Do you want to play again? (y/n)")
ans = input()
if ans == 'n' or ans == 'N':
break
🎉 That’s it! Isn’t it easy? Now you have your own game to play!
You can also refer my GitHub repo.
How was the article ? Feedbacks are warmly welcomed.
Previously published at https://medium.com/@theavidcoder/make-your-own-game-in-python-ec0b83f08d35