Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 32 additions & 37 deletions rock_paper_scissor.py
Original file line number Diff line number Diff line change
@@ -1,46 +1,41 @@
import random

ROCK = 'rock'
PAPER = 'paper'
SCISSOR = 'scissor'
choices = [ROCK, PAPER, SCISSOR]
positive = [[PAPER, ROCK], [SCISSOR, PAPER], [ROCK, SCISSOR]]
negative = [[ROCK, PAPER], [PAPER, SCISSOR], [SCISSOR, ROCK]]
choices = ['rock', 'paper', 'scissor']

# What beats what
win_map = {
'rock': 'scissor',
'paper': 'rock',
'scissor': 'paper'
}

def get_computer_move():
return random.choice(choices)

def find_winner(user_move, computer_move):
if [user_move, computer_move] in positive:
return 1
elif [user_move, computer_move] in negative:
return -1
return 0
def find_winner(user, comp):
if user == comp:
return "Tie"
elif win_map[user] == comp:
return "User Won"
else:
return "Computer Won"

print("===== Rock Paper Scissor =====")

print("===== Welcome to Rock, Paper And Scissor Game =====")
while True:
choice = input("Do you wanna play (y/n): ").strip().lower()
if choice == 'y':
computer_move = get_computer_move()
while True:
move = input("Select a move ('r' for rock/'p' for paper/'s' for scissor): ").strip().lower()
if move in ['r', 'p', 's']:
user_move = {'r': ROCK, 'p': PAPER, 's': SCISSOR}[move]
print(f"User Move: {user_move}")
print(f"Computer's Move: {computer_move}")
output = find_winner(user_move, computer_move)
if output == 1:
print("User Won !!!")
elif output == -1:
print("Computer Won !!!")
else:
print("It's a Tie !!!")
break
else:
print("Invalid input...please try again")
elif choice == 'n':
print("Exiting... Thanks for playing!")
if input("Play? (y/n): ").lower() != 'y':
print("Bye")
break
else:
print("Invalid input...please try again")
print()

move = input("r/p/s: ").lower()

if move not in ['r', 'p', 's']:
print("Invalid input\n")
continue

user_move = {'r': 'rock', 'p': 'paper', 's': 'scissor'}[move]
computer_move = get_computer_move()

print("User:", user_move)
print("Computer:", computer_move)
print(find_winner(user_move, computer_move), "\n")