Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

rock paper scissors game in python

import random
choices = ['r', 's', 'p']
choice_meaning = {
    'r': 'Rock',
    's': 'Scissors',
    'p': 'Paper'
}
ai_choice = random.choice(choices)

user_choice = input('Enter your choice as r = Rock, s= scissors, p= paper : ')

if user_choice in choices:
    print(
        f"Your choice is {choice_meaning[user_choice]}.Computer choice is {choice_meaning[ai_choice]}.")
    if user_choice == ai_choice:
        print("Tie")
    elif (user_choice == 'r' and ai_choice == 's') or (user_choice == 's' and ai_choice == 'p') or (user_choice == 'p' and ai_choice == 'r'):
        print("You won")
    else:
        print("You lost")
else:
    print("Invalid inpute")
Comment

python rock paper scissors


import random
import time

computerwins = 0
playerwins = 0
ties = 0
end = 0

while True:

    choices = ["rock",
               "paper",
               "scissors"]

    userChoice = raw_input("Rock, Paper, Scissors, or End")

    computerChoice = (random.choice(choices))
    print(computerChoice)

    if userChoice == computerChoice:
        time.sleep(0.5)
        print("Tie!
")
        ties += 1
        end += 1

    elif userChoice == "rock":
        if computerChoice == "paper":
            time.sleep(0.5)
            print("Computer Win!
")
            computerwins +=1
            end += 1

        else:
            time.sleep(0.5)
            print("Player win!
")
            playerwins += 1
            end += 1

    elif userChoice == "paper":
        if computerChoice == "rock":
            time.sleep(0.5)
            print("Player win!
")
            playerwins += 1
            end += 1

        else:
            time.sleep(0.5)
            print("Computer win!
")
            computerwins += 1
            end += 1

    elif userChoice == "scissors":
        if computerChoice == "rock":
            time.sleep(0.5)
            print("Computer win!
")
            computerwins += 1
            end += 1

        else:
            time.sleep(0.5)
            print("Player win!
")
            playerwins += 1
            end += 1

    elif userChoice == "end":
            choices.append("end")
            print("
Great game!
")
            print("Total score for computer: ", computerwins, "wins!")
            print("Total score for player: ", playerwins, "wins!")
            print("Total ties: ", ties, "ties!")
            time.sleep(2)
            break
Comment

python rock paper scissors game

import random as rd

objects = ['rock', 'paper', "scissor"] # Rock: 0, Paper: 1, Scissor: 2
condition = {'0,2': 'rock', '2,0': 'rock', '0,1': 'paper', '1,0': 'paper', '1,2': 'scissor', '2,1': 'scissor'}
rounds = int(input('How many rounds(int): '))
game = 0
drew = 0
count = 0

def play():
    global rounds, game, count, drew
    while game < rounds:
        game += 1
        print('Game %d'.center(20, "-") % game)
        player_move = objects.index(input("Rock, Paper, or Scissor: ").lower())
        computer_move = rd.randint(0,2) 
        print("Your selection: %s, Computer's selection: %s" % (objects[player_move], objects[computer_move]))
        if player_move == computer_move:
            drew += 1
            print('Game %d: Drew!' % game)
            continue
        winning_move = [str(player_move) + ',' + str(computer_move)]
        print(condition[''.join(winning_move)], player_move)
        if condition[''.join(winning_move)] == objects[player_move]:
            count += 1
            print('Game %d: Player Wins!' % game)
        if condition[''.join(winning_move)] == objects[computer_move]:
            print('Game %d: Computer Wins!' % game)

    if count > (rounds-drew)/2:
        return 'Final Conclusion: Player Wins'
    if count == (rounds-drew)/2:
        return 'Final Conclusion: Drew'
    else:
        return 'Final Conclusion: Computer Wins'

print(play())
Comment

rock paper scissors game in python gui

from tkinter import *
import random
import time
window = Tk()
window.geometry('900x700')
window.config(bg='#212120')
window.title('Rock,paper and scissors game')
#main game
def start():
    la = Label(window,text='choose one',font=(50),fg='black')
    la.pack()
    def rock():
        a = ['rock', 'paper', 'scissors']
        b = random.choice(a)
        if b == 'scissors':
            global l
            l = Label(window,text='you win',fg='#46ff03',font=(40))
            l.pack()
        elif b == 'paper':
            l = Label(window,text='you lose',fg='#ff0f03',font=(40))
            l.pack()
        elif b == 'rock':
            l= Label(window,text='tie',fg='#2c03fc',font=(40))
            l.pack()
        but1.destroy()
        but2.destroy()
        but3.destroy()
        global c
        c = Label(window, text='computer: ' + b, fg='black')
        c.pack()
        c.place(x=600, y=50)
        time.sleep(1)
        la.destroy()
        play_again()
    def scissors():
        a = ['rock', 'paper', 'scissors']
        b = random.choice(a)
        if b == 'paper':
            global l
            l = Label(window, text='you win', fg='#46ff03',font=(40))
            l.pack()
        elif b == 'rock':
            l = Label(window, text='you lose', fg='#ff0f03',font=(40))
            l.pack()
        elif b == 'scissors':
            l = Label(window, text='tie', fg='#2c03fc',font=(40))
            l.pack()
        but1.destroy()
        but2.destroy()
        but3.destroy()
        global c
        c = Label(window, text='computer: ' + b, fg='black')
        c.pack()
        c.place(x=600, y=50)
        time.sleep(1)
        la.destroy()
        play_again()
    def paper():
        a = ['rock', 'paper', 'scissors']
        b = random.choice(a)
        if b == 'rock':
            global l
            l = Label(window, text='you win', fg='#46ff03',font=(40))
            l.pack()
        elif b == 'scissors':
            l = Label(window, text='you lose', fg='#ff0f03',font=(40))
            l.pack()
        elif b == 'paper':
            l = Label(window, text='tie', fg='#2c03fc',font=(40))
            l.pack()
        but1.destroy()
        but2.destroy()
        but3.destroy()
        global c
        c = Label(window, text='computer: ' + b, fg='black')
        c.pack()
        c.place(x=600,y=50)
        time.sleep(1)
        la.destroy()
        play_again()
    but1 = Button(window,text="Rock",command=rock,fg='#1c1f1f')
    but2 = Button(window,text="scissors",command=scissors,fg='#1c1f1f')
    but3 = Button(window,text="paper",command=paper,fg='#1c1f1f')
    but1.pack()
    but3.pack()
    but2.pack()
    but1.place(x=100,y=100)
    but2.place(x=800,y=100)
    but3.place(x=450,y=100)
    button.destroy()
def play_again():
    def yes():
        start()
        c.destroy()
        l.destroy()
        label1.destroy()
        button1.destroy()
        button2.destroy()
    def no():
        exit()

    label1 = Label(window,text='play again?',padx=20,pady=20)
    button1 = Button(window,text='yes',command=yes)
    button2= Button(window,text='no',command=no)
    label1.pack()
    button1.pack()
    button2.pack()
    button2.place(x=450,y=100)
    button1.place(x=400,y=100)
button = Button(window,text='start the game',command=start)
button.pack()
window.mainloop()
window.destroy()
Comment

1 line rock paper scissors python

winner = (3 + player1 - player2) % 3;
# Rock=0, Paper=1, Scissors=2
# 1 if player1 wins, 2 if player2 wins, 0 if draw
Comment

How to code a simple rock, paper, scissors game on Python

import random

your_input = input("Enter a choice (r = rock, p = paper, s = scissors): ")
user = ""
if your_input.lower() == "r":
    user = "rock"
if your_input.lower() == "p":
    user = "paper"
if your_input.lower() == "s":
    user = "scissors"

possible = ["rock", "paper", "scissors"]
computer = random.choice(possible)

print(f"
You chose {user}, computer chose {computer}.
")

if user == computer:
    print(f"Both players selected {user}. It's a tie!")
elif user == "rock":
    if computer == "scissors":
        print("Rock smashes scissors! You win!")
    else:
        print("Paper covers rock! You lose!")
elif user == "paper":
    if computer == "rock":
        print("Paper covers rock, You win!")
    else:
        print("Scissors cuts paper! You lose!")
elif user == "scissors":
    if computer == "paper":
        print("Scissors cuts paper! You win!")
    else:
        print("Rock smashes scissors! You lose!")
Comment

PREVIOUS NEXT
Code Example
Python :: how to remove last 2 rows in a dataframe 
Python :: turn off colorbar seaborn heatmap 
Python :: python set cookies 
Python :: run python script without .py 
Python :: create jwt token in django 
Python :: python convert 12 hour time to 24 hour 
Python :: how to make a loop in python 
Python :: how to convert categorical data to numerical data in python 
Python :: Python How to convert a string to the name of a function? 
Python :: Iterate through string in python using for loop and rang 
Python :: steps in for loop python 
Python :: flip dictionary python 
Python :: matplotlib temperature celsius 
Python :: every cell change comma to point pandas 
Python :: what is the best ide for python 
Python :: python calculated row in dataframe subtract 
Python :: instalar sympy en thonny 
Python :: how to close ursina screen 
Python :: install nsml python 
Python :: gunicorn django static files 
Python :: Failed to build wxPython 
Python :: simple keras model with one layer 
Python :: email confirmation django 
Python :: dense in keras 
Python :: set page title name and favicon in streamlit 
Python :: python Python Program to Catch Multiple Exceptions in One Line 
Python :: expanding nebula foobar 
Python :: get the creating date of files ftp python 
Python :: class python __call__ 
Python :: python custom class indexing 
ADD CONTENT
Topic
Content
Source link
Name
9+6 =