DekGenius.com
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")
rock paper scissors python
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
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())
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!")
© 2022 Copyright:
DekGenius.com