Python code for Rock Paper Scissors Game
Following is code for Rock-Paper-Scissors on command line interface:
print("Rock, Paper, Scissors")
while True:
print("Enter choice \n1 for Rock \n2 for Paper \n3 for Scissors")
choice = int(input("User turn: "))
while choice > 3 or choice < 1:
choice = int(input("Enter valid input: "))
if choice == 1:
choice_name = 'Rock'
elif choice == 2:
choice_name = 'Paper'
else:
choice_name = 'Scissors'
print("User has chosen: " + choice_name)
print("Now it's the computer's turn...")
import random
comp_choice = random.randint(1,3)
if comp_choice == 1:
comp_choice_name = 'Rock'
elif comp_choice == 2:
comp_choice_name = 'Paper'
else:
comp_choice_name = 'Scissors'
print("Computer has chosen: " + comp_choice_name)
print(choice_name + " V/S " + comp_choice_name)
result = None
if((choice == 1 and comp_choice == 2) or (choice == 2 and comp_choice ==1)):
result = "Paper"
elif((choice == 1 and comp_choice == 3) or (choice == 3 and comp_choice == 1)):
result = "Rock"
else:
result = "Scissors"
if result == choice_name:
print("User wins!")
else:
print("Computer wins!")
print("Do you want to play again? (Y/N)")
ans = input()
if ans == 'n' or ans == 'N':
break
print("\nThanks for playing!")
In this code, the user is prompted to enter their choice (Rock, Paper, or Scissors), and the computer makes its choice randomly. The result is then displayed, and the user is given the option to play again.
Following is code for Rock Paper Scissors with Graphical User Interface:
import tkinter as tk
import random
def play_game():
user_choice = var.get()
comp_choice = random.randint(1,3)
if user_choice == 1:
choice_name = 'Rock'
elif user_choice == 2:
choice_name = 'Paper'
else:
choice_name = 'Scissors'
if comp_choice == 1:
comp_choice_name = 'Rock'
elif comp_choice == 2:
comp_choice_name = 'Paper'
else:
comp_choice_name = 'Scissors'
result = None
if((user_choice == 1 and comp_choice == 2) or (user_choice == 2 and comp_choice ==1)):
result = "Paper"
elif((user_choice == 1 and comp_choice == 3) or (user_choice == 3 and comp_choice == 1)):
result = "Rock"
else:
result = "Scissors"
if result == choice_name:
result_text.configure(text="User wins!",fg='green')
else:
result_text.configure(text="Computer wins!",fg='red')
user_choice_text.configure(text="User has chosen: " + choice_name)
comp_choice_text.configure(text="Computer has chosen: " + comp_choice_name)
root = tk.Tk()
root.title(“Rock-Paper-Scissors”)
root.geometry(“400×300”)
var = tk.IntVar()
label = tk.Label(root, text=”Choose your weapon:”, font=(“Helvetica”, 14))
label.pack()
rock_button = tk.Radiobutton(root, text=”Rock”, variable=var, value=1, command=play_game)
rock_button.pack()
paper_button = tk.Radiobutton(root, text=”Paper”, variable=var, value=2, command=play_game)
paper_button.pack()
scissors_button = tk.Radiobutton(root, text=”Scissors”, variable=var, value=3, command=play_game)
scissors_button.pack()
user_choice_text = tk.Label(root, text=””, font=(“Helvetica”, 14))
user_choice_text.pack()
comp_choice_text = tk.Label(root, text=””, font=(“Helvetica”, 14))
comp_choice_text.pack()
result_text = tk.Label(root, text=””, font=(“Helvetica”, 14))
result_text.pack()
root.mainloop()
In this code, the user makes their choice by selecting one of the three radio buttons (Rock, Paper, or Scissors). The computer’s choice is generated randomly, and the result is displayed using labels. The text color of the result changes based on who wins (green for the user, red for the computer).