I made rock/paper/scissors in short time
Code:
#imports
import time, random, sys, getpass
#variables
pause = 1
player_1 = “”
player_2 = “”
#define what’s done when the player wins
def win(player_w, player_l):
print(f”Player {player_w} defeats {player_l}!”)
while True:
choice = str(input(“Do you want to play again?\n>\t”))
if “y” in choice.lower() or “yes” in choice.lower():
break
elif “n”in choice.lower() or “no” in choice.lower():
sys.exit()
else:
print(“Invalid answer, try again”)
#define what’s done when the player loses
def draw():
print(“DRAW!”)
while True:
choice = str(input(“Do you want to play again?\n> “))
if “y” in choice.lower() or “yes” in choice.lower():
break
elif “n”in choice.lower() or “no” in choice.lower():
sys.exit()
else:
print(“Invalid answer, try again”)
#main
while True:
#make the choice of playing optional
while True:
choice = str(input(“Are you ready?\n>\t”))
if “y” in choice.lower() or “yes” in choice.lower():
print(“Great!”)
time.sleep(pause)
break
elif “n”in choice.lower() or “no” in choice.lower():
print(“Pussies”)
time.sleep(pause)
sys.exit()
else:
print(“Invalid answer, try again”)
#get first player’s choice
while True:
player_1 = str(getpass.getpass(“Player 1, choose\n>\t”))
if player_1.lower() == “rock” or player_1.lower() == “paper” or player_1.lower() == “scissors”:
print(“Got it”)
time.sleep(pause)
break
else:
print(“Invalid choice, try again”)
#get second player’s choice
while True:
player_2 = str(getpass.getpass(“Player 2, choose\n>\t”))
if player_2.lower() == “rock” or player_2.lower() == “paper” or player_2.lower() == “scissors”:
print(“Got it”)
time.sleep(pause)
break
else:
print(“Invalid choice, try again”)
if player_1 == player_2:
draw()
elif player_1 == “rock” and player_2 == “scissors”:
win(1,2)
elif player_1 == “paper” and player_2 == “rock”:
win(1,2)
elif player_1 == “scissors” and player_2 == “paper”:
win(1,2)
elif player_2 == “rock” and player_1 == “scissors”:
win(2,1)
elif player_2 == “paper” and player_1 == “rock”:
win(2,1)
elif player_2 == “scissors” and player_1 == “paper”:
win(2,1)
Leave a Reply