Rock, Paper, Scissors and a Restart Option - python

I am just beginning to use python and could use some help! I was working on a rock, paper, scissors game and I wanted to add a restart option once either the human or computer reaches 3 wins.
I have looked all over for some answers but from all the other code I looked at seemed way out of my league or extremely different from what I wrote. I haven't tried using def and classes, which I saw a lot of, and made it look very simple. I know I'm probably going about this in a really round about way but I just want to finish this without completely copying someone's code.
import random
moves = ["Rock", "Paper", "Scissors"]
player_score = 0
computer_score = 0
draws = 0
keep_playing = True
while keep_playing == True:
cmove = random.choice(moves)
pmove = input("What is your move: Rock, Paper, or Scissors?")
print("The computer chose", cmove)
#Logic to game
if cmove == pmove:
print("It's a DRAW!")
elif pmove == "Rock" and cmove == "Scissors":
print("--Player Wins!--")
elif pmove == "Rock" and cmove == "Paper":
print("--Computer Wins!--")
elif cmove == "Paper" and cmove == "Rock":
print("--Player Wins!--")
elif pmove == "Paper" and cmove == "Scissors":
print("--Computer Wins!--")
elif pmove == "Scissors" and cmove == "Paper":
print("--Player Wins!--")
elif pmove == "Scissors" and cmove == "Rock":
print("--Computer Wins!--")
#Scoreboard
if pmove == cmove:
draws = draws + 1
print("Player:" + str(player_score) + ' | ' + "Computer:" + str(computer_score) + ' | ' + "Draws:" + str(draws))
if pmove == "Rock" and cmove == "Scissors":
player_score = player_score + 1
print("Player:" + str(player_score) + ' | ' + "Computer:" + str(computer_score) + ' | ' + "Draws:" + str(draws))
if pmove == "Rock" and cmove == "Paper":
computer_score = computer_score + 1
print("Player:" + str(player_score) + ' | ' + "Computer:" + str(computer_score) + ' | ' + "Draws:" + str(draws))
if pmove == "Paper" and cmove == "Rock":
player_score = player_score + 1
print("Player:" + str(player_score) + ' | ' + "Computer:" + str(computer_score) + ' | ' + "Draws:" + str(draws))
if pmove == "Paper" and cmove == "Scissors":
computer_score = computer_score + 1
print("Player:" + str(player_score) + ' | ' + "Computer:" + str(computer_score) + ' | ' + "Draws:" + str(draws))
if pmove == "Scissors" and cmove == "Paper":
player_score = player_score + 1
print("Player:" + str(player_score) + ' | ' + "Computer:" + str(computer_score) + ' | ' + "Draws:" + str(draws))
if pmove == "Scissors" and cmove == "Rock":
computer_score = computer_score + 1
print("Player:" + str(player_score) + ' | ' + "Computer:" + str(computer_score) + ' | ' + "Draws:" + str(draws))
#Win/lose restart point?
if player_score == 3:
print("-----You Win!-----")
break
if computer_score == 3:
print("-----You Lose!-----")
break
I want the code to end saying, "You Win!" or "You Lose!"and then ask for an input to whether or not they want to restart and then it resets the scores and keeps going or if they say no it breaks.

You already have the loop to do this. So when you want to "restart" your game, you really just need to reset the scores.
Starting from your win/lose conditions:
#Win/lose restart point?
if player_score == 3:
print("-----You Win!-----")
replay = input("Would you like to play again?")
if replay.upper().startswith('Y'):
player_score = 0
computer_score = 0
draws = 0
else:
keep_playing = False
if computer_score == 3:
print("-----You Lose!-----")
if replay.upper().startswith('Y'):
player_score = 0
computer_score = 0
draws = 0
else:
keep_playing = False

You need to get user input in the while loop and set keep_playing accordingly.
while keep_playing == True:
cmove = random.choice(moves)
pmove = input("What is your move: Rock, Paper, or Scissors?")
print("The computer chose", cmove)
#Logic to game
if cmove == pmove:
print("It's a DRAW!")
elif pmove == "Rock" and cmove == "Scissors":
print("--Player Wins!--")
elif pmove == "Rock" and cmove == "Paper":
print("--Computer Wins!--")
elif cmove == "Paper" and cmove == "Rock":
print("--Player Wins!--")
elif pmove == "Paper" and cmove == "Scissors":
print("--Computer Wins!--")
elif pmove == "Scissors" and cmove == "Paper":
print("--Player Wins!--")
elif pmove == "Scissors" and cmove == "Rock":
print("--Computer Wins!--")
# new code
play_again = input("Would you like to play again?")
keep_playing = play_again.lower().startswith('y') # this will be set to False if the user's input doesn't start with 'y', the loop will exit

Related

How do I save scores in an external text file from a rock, paper, scissors, game?

I am very new to python so I apologise for that. anyway I have programmed a simple rock, paper, scissors game where you play against a bot that randomly pulls "rock", "paper", or "scissors" from a list. The game (if you can call it that) works fine, However, I want to be able to save the game score to a text file, preferably called "score.txt", when the game ends, I have already tried doing this and it failed miserably, Heres the output from "score.txt"
Test | | # theres meant to be a "Score" and a Win, Loss, and Tie in between
Test | | # the | |, for e.g, Test | Win | 5 - Score
Test | | # Test | Win | 10
Test | | # Test | Loss | 5
and heres the code for the program I made:
import random
rps = ["rock","paper","scissors"]
score = "score"
score1 = 0
name = None
i = None
f = open("score.txt")
while not name:
name = str(input("Enter a name: "))
for i in range(4):
while not (i == "rock" or i == "paper" or i == "scissors"):
i = str(input("Enter rock paper or scissors: "))
choice_rps = random.choice(rps)
print(choice_rps)
if i == "rock" and choice_rps == "paper":
result = "lost"
print("You lost :(")
elif i == "paper" and choice_rps == "rock":
result = "won"
print("You won!!!")
elif i == "paper" and choice_rps == "scissors":
result = "lost"
print("You lost :(")
elif i == "scissors" and choice_rps == "paper":
result = "won"
print("You won!!!!!!!")
elif i == "scissors" and choice_rps == "rock":
result = "lost"
print("You lost :(")
elif i == "rock" and choice_rps == "scissors":
result = "won"
print("You won!!!!!!")
else:
result = "tie"
print("It's a tie")
if result == "won":
print("score + 5")
score1 = score1 + 5
f.write("\n" + name + " | Win | " + str(score1))
elif result == "lost":
print("score - 5")
score1 = score1 - 5
f.write("\n" + name + " | Loss | " + str(score1))
elif result == "tie":
print("score stays same")
score1 = score1
Apologises if this seemms confusing to read. Any advice would be greatly appreciated.
(I'm ten years old so more towards a simple answer would be appreciated :)
There's three issues with your code:
open() takes in a second parameter that describes whether we want to read, write, or append to a file. In this case, we want to write to the file, so we should use open('score.txt', 'w') -- the w is for write.
You need to close your file after you're done writing to it using f.close().
The code to actually write to the file needs to be shifted over one indent. Otherwise, if we win, the score handling code will never be executed (since it's under an else branch that handles ties).
Here's the code with all three fixes:
import random
rps = ["rock","paper","scissors"]
score = "score"
score1 = 0
name = None
i = None
f = open("score.txt", "w")
while not name:
name = str(input("Enter a name: "))
for i in range(4):
while not (i == "rock" or i == "paper" or i == "scissors"):
i = str(input("Enter rock paper or scissors: "))
choice_rps = random.choice(rps)
print(choice_rps)
if i == "rock" and choice_rps == "paper":
result = "lost"
print("You lost :(")
elif i == "paper" and choice_rps == "rock":
result = "won"
print("You won!!!")
elif i == "paper" and choice_rps == "scissors":
result = "lost"
print("You lost :(")
elif i == "scissors" and choice_rps == "paper":
result = "won"
print("You won!!!!!!!")
elif i == "scissors" and choice_rps == "rock":
result = "lost"
print("You lost :(")
elif i == "rock" and choice_rps == "scissors":
result = "won"
print("You won!!!!!!")
else:
result = "tie"
print("It's a tie")
if result == "won":
print("score + 5")
score1 = score1 + 5
f.write("\n" + name + " | Win | " + str(score1))
elif result == "lost":
print("score - 5")
score1 = score1 - 5
f.write("\n" + name + " | Loss | " + str(score1))
elif result == "tie":
print("score stays same")
score1 = score1
f.close()
Edits in the code:
1- with open("score.txt", "a") as f: # not to use close func, "a" is to append new values, use "w" if you want to rewrite the whole file
2- indentation fixed
3- using string format allows you to print a dynamic string
here is a working version of your code:
import random
rps = ["rock", "paper", "scissors"]
score = "score"
score1 = 0
name = None
i = None
with open("score.txt", "a") as f: # not to use close func, "a" is to append new values, use "w" if you want to rewrite the whole file
while not name:
name = str(input("Enter a name: "))
for i in range(4):
while not (i == "rock" or i == "paper" or i == "scissors"):
i = str(input("Enter rock paper or scissors: "))
choice_rps = random.choice(rps)
print(choice_rps)
if i == "rock" and choice_rps == "paper":
result = "lost"
print("You lost :(")
elif i == "paper" and choice_rps == "rock":
result = "won"
print("You won!!!")
elif i == "paper" and choice_rps == "scissors":
result = "lost"
print("You lost :(")
elif i == "scissors" and choice_rps == "paper":
result = "won"
print("You won!!!!!!!")
elif i == "scissors" and choice_rps == "rock":
result = "lost"
print("You lost :(")
elif i == "rock" and choice_rps == "scissors":
result = "won"
print("You won!!!!!!")
else:
result = "tie"
print("It's a tie")
if result == "won": #indentation fixed from here to the end
print("score + 5")
score1 = score1 + 5
f.write(f"{name} | Win | {str(score1)} \n")#using string format allows you to have a dynamic string
elif result == "lost":
print("score - 5")
score1 = score1 - 5
f.write(f"{name} | Lose | {str(score1)} \n")
elif result == "tie":
print("score stays same")
score1 = score1

Rock Paper Scissors, but wait! It's set up differently

Alice and Bob are playing rock-paper-scissors game.
Input Specification
The first line contains one integer N (1≤N≤100) that represents the number of games.
The second line is Alice's shape sequence and third line is Bob's shape sequence.
Output Specification
Two integers separated by a space, representing the number of games won by Alice and the number of games won by Bob.
I first made Alice and Bob's shapes in a list using .split() and then I set up a bunch of elif and if statements for each possible scenario. But the problem I'm having is that it is not iterating correctly for example if I input
3
rock rock rock
rock rock paper
instead of output being 0, 1 I get 0,0 and I can't seem to figure out why. Here's my code below.
games = input()
alice = input()
bob = input()
alice = alice.split()
bob = bob.split()
awin = []
bwin = []
a = 0
while a < len(alice):
for i in range(len(alice)):
if alice[i] == "rock" and bob[i] == "rock":
break
elif alice[i] == "scissors" and bob[i] == "scissors":
break
elif alice[i] == "paper" and bob[i] == "paper":
break
elif alice[i] == "rock" and bob[i] == "scissors":
awin.append('rock beat scissors')
break
elif alice[i] == "rock" and bob[i] == "paper":
bwin.append('paper beat rock')
break
elif alice[i] == "paper" and bob[i] == "rock":
awin.append('paper beat rock')
break
elif alice[i] == "paper" and bob[i] == "scissors":
bwin.append('scissors beat paper')
break
elif alice[i] == "scissors" and bob[i] == "paper":
awin.append('scissors beat paper')
break
elif alice[i] == "scissors" and bob[i] == "rock":
bwin.append('rock beat scissors')
break
i += 1
a+=1
print('output:')
print(awin)
print(bwin)
print(str(len(awin)) + " " + str(len(bwin)))
Here's a more simplified version of your code:
games = input()
alice = input()
bob = input()
alice = alice.split()
bob = bob.split()
awin = []
bwin = []
for i in range(len(alice)):
# If both players play the same move
if alice[i] == bob[i]:
continue
elif alice[i] == "rock" and bob[i] == "scissors":
awin.append('rock beat scissors')
elif alice[i] == "rock" and bob[i] == "paper":
bwin.append('paper beat rock')
elif alice[i] == "paper" and bob[i] == "rock":
awin.append('paper beat rock')
elif alice[i] == "paper" and bob[i] == "scissors":
bwin.append('scissors beat paper')
elif alice[i] == "scissors" and bob[i] == "paper":
awin.append('scissors beat paper')
elif alice[i] == "scissors" and bob[i] == "rock":
bwin.append('rock beat scissors')
print('output:')
print(awin)
print(bwin)
print(str(len(awin)) + " " + str(len(bwin)))
Others have given some direction on the original code. I'm just stopping by to offer an alternative approach that is a little more 'pythonic'...and because coding games like this can be a bit of fun!
This implementation doesn't play the same as code in the original question, but has the advantage of following how the game flows in real life.
from collections import defaultdict
# defaultdict returns 0 (int() initial value) if the key does
# not exist. So, encode contests in format AB where 1 means
# A beats B. All other scenarios default to 0 i.e. B wins.
game = defaultdict(int)
game['RS'] = 1
game['SP'] = 1
game['PR'] = 1
num_games = input()
aw,bw = 0,0
while num_games:
alice = raw_input('Shape for Alice:')
bob = raw_input('Shape for Bob:')
aw += game[alice + bob]
bw += game[bob + alice]
num_games -= 1
print(aw,bw)

How to use variables in multiple different functions in Python 3

I define a variable named 'computerChoice' in a function and then attempt to use it in another variable but it says that 'computerChoice' is undefined... Being new to Python, I am not sure why this is happening so I hope that you could help answer my question!
def computerDecision():
import random
for x in range(1):
num = random.randint(1,4)
if num == 1:
computerChoice = 'rock'
elif num == 2:
computerChoice = 'paper'
elif num == 3:
computerChoice = 'scissors'
def determineWinner():
if userInput == computerChoice:
print("You both chose " + computerChoice)
print("DRAW!")
elif userInput == 'rock' and computerChoice == 'paper':
print("The computer chose " + computerChoice)
print("COMPUTER WINS!")
elif userInput == 'rock' and computerChoice == 'scissors':
print("The computer chose " + computerChoice)
print('USER WINS!')
elif userInput == 'paper' and computerChoice == 'rock':
print("The computer chose " + computerChoice)
print("USER WINS!")
elif userInput == 'paper' and computerChoice == 'scissors':
print("The computer chose " + computerChoice)
print("COMPUTER WINS!")
elif userInput == 'scissors' and computerChoice == 'rock':
print("The computer chose " + computerChoice)
print("COMPUTER WINS!")
elif userInput == 'scissors' and computerChoice == 'paper':
print("The computer chose " + computerChoice)
print("USER WINS!")
The computerChoice variable is restricted in scope to the function in which it was defined. This is to prevent functions from interfering with one another. You can declare it to be global (accessible from anywhere) with the keyword global:
global computerChoice
This is probably not what you want, however, as your functions do not need to interact with each other. You can just make computerDecision() return its choice.
import random
def computerDecision():
for x in range(1):
num = random.randint(1,4)
if num == 1:
return 'rock'
elif num == 2:
return 'paper'
elif num == 3:
return 'scissors'
def determineWinner():
computerChoice = computerDecision()
if userInput == computerChoice:
print("You both chose " + computerChoice)
print("DRAW!")
elif userInput == 'rock' and computerChoice == 'paper':
print("The computer chose " + computerChoice)
print("COMPUTER WINS!")
elif userInput == 'rock' and computerChoice == 'scissors':
print("The computer chose " + computerChoice)
print('USER WINS!')
elif userInput == 'paper' and computerChoice == 'rock':
print("The computer chose " + computerChoice)
print("USER WINS!")
elif userInput == 'paper' and computerChoice == 'scissors':
print("The computer chose " + computerChoice)
print("COMPUTER WINS!")
elif userInput == 'scissors' and computerChoice == 'rock':
print("The computer chose " + computerChoice)
print("COMPUTER WINS!")
elif userInput == 'scissors' and computerChoice == 'paper':
print("The computer chose " + computerChoice)
print("USER WINS!")
Also note that computerDecision() can be redefined more simply, as just return random.choice(("rock", "paper", "scissors")).

Python Coding Rock,Paper,Scissors,Lizard and Spock [duplicate]

This question already has an answer here:
How can I concatenate str and int objects?
(1 answer)
Closed 6 years ago.
total_guess = 0
wins = 0
loss = 0
import random
characters = ["rock", "paper", "scissors", "lizard", "spock"]
computer = characters[random.randint(0,4)]
print(computer)
Subroutine- functions fine
def valid(text, flag):
error_message= ""
while True:
var = input(error_message + text)
if flag == "s":
if var.isalpha()==True:
break
else:
error_message = "This is not valid, "
elif flag =="i":
if var.isdigit()==True:
var = int(var)
break
else:
error_message = user_name + " this is not a number, "
elif flag == "g":
if var == "rock" or var == "paper" or var == "scissors" or var == "lizard" or var == "spock":
break
else:
error_message = user_name + " this is not valid! "
return(var)
user_name = valid("What is your name?", "s")
num_rounds = valid(user_name +" how many rounds do you want?", "i")
This code does not work either due to past changes initially worked, 'says can't convert 'int' object to str implicitly
while True:
player = valid(user_name + """ ,What do you want as your character:
Rock, paper, scissors, lizard or spock""", "g" )
while num_rounds > total_guess:
total_guess = total_guess + 1
if player == computer:
print("Draw!")
# --------------------------------------------
elif player == "Rock" or player == "rock":
if computer == "paper" or computer == "spock" :
loss = loss + 1
print("You lost ", computer, " beats ", player)
print( user_name + " you have won " + wins +" games")
if computer == "scissors" or computer == "lizard":
wins = wins + 1
print("You win", player, " beats ", computer)
# ---------------------------------------------
elif player == "Paper" or player == "paper":
if computer == "scissors" or computer == "lizard":
loss = loss + 1
print("You lost ", computer, " beats ", player)
if computer == "rock" or computer == "spock":
wins = wins + 1
print("You win", player, " beats ", computer)
# ---------------------------------------------
elif player == "Scissors" or player == "scissors":
if computer =="Spock" or computer == "rock":
loss = loss + 1
print("You lost ", computer, " beats ", player)
if computer =="paper" or computer == "lizard":
wins = wins + 1
print("You win", player, " beats ", computer)
# --------------------------------------------
elif player == "Lizard" or player =="lizard":
if computer =="scissors" or computer == "rock":
loss = loss + 1
print("You lost ", computer, " beats ", player)
if computer == "paper" or computer == "spock":
wins = wins + 1
print("You win", player, " beats ", computer)
# --------------------------------------------
elif player == "Spock" or player == "spock":
if computer == "lizard" or computer == "paper":
loss = loss + 1
print("You lost ", computer, " beats ", player)
if computer =="rock" or computer == "scissors":
wins = wins + 1
print("You win", player, " beats ", computer)
# -------------------------------------------
This block code to restart game does not work it's purpose is to have a try again feature
end_game = input("To exit enter N, to play again enter any key ")
if end_game == 'n' or end_game == 'N':
print("THANKS FOR PLAYING " + user_name + '!')
break
This could be shortened heavily but I don't feel like rewriting your entire program. This should work as expected.
total_guess = 0
wins = 0
loss = 0
import random
characters = ["rock", "paper", "scissors", "lizard", "spock"]
computer = characters[random.randint(0,4)]
print(computer)
def valid(text, flag):
error_message= ""
while True:
var = input(error_message + text)
if flag == "s":
if var.isalpha()==True:
break
else:
error_message = "This is not valid, "
elif flag =="i":
if var.isdigit()==True:
var = int(var)
break
else:
error_message = user_name + " this is not a number, "
elif flag == "g":
if var == "rock" or var == "paper" or var == "scissors" or var == "lizard" or var == "spock":
break
else:
error_message = user_name + " this is not valid! "
return(var)
user_name = valid("What is your name?", "s")
num_rounds = valid(user_name +" how many rounds do you want?", "i")
while True:
while num_rounds > total_guess:
player = valid(user_name + """ ,What do you want as your character:
Rock, paper, scissors, lizard or spock""", "g" )
total_guess = total_guess + 1
if player == computer:
print("Draw!")
# --------------------------------------------
elif player == "Rock" or player == "rock":
if computer == "paper" or computer == "spock" :
loss = loss + 1
print(' '.join(("You lost", computer, "beats", player)))
if computer == "scissors" or computer == "lizard":
wins = wins + 1
print(' '.join(("You win", player, "beats", computer)))
elif player == "Paper" or player == "paper":
if computer == "scissors" or computer == "lizard":
loss = loss + 1
print(' '.join(("You lost", computer, "beats", player)))
if computer == "rock" or computer == "spock":
wins = wins + 1
print(' '.join(("You win", player, "beats", computer)))
elif player == "Scissors" or player == "scissors":
if computer =="Spock" or computer == "rock":
loss = loss + 1
print(' '.join(("You lost", computer, " beats ", player)))
if computer =="paper" or computer == "lizard":
wins = wins + 1
print(' '.join(("You win", player, "beats", computer)))
elif player == "Lizard" or player =="lizard":
if computer =="scissors" or computer == "rock":
loss = loss + 1
print(' '.join(("You lost", computer, "beats", player)))
if computer == "paper" or computer == "spock":
wins = wins + 1
print(' '.join(("You win", player, "beats", computer)))
elif player == "Spock" or player == "spock":
if computer == "lizard" or computer == "paper":
loss = loss + 1
print(' '.join(("You lost", computer, "beats", player)))
if computer =="rock" or computer == "scissors":
wins = wins + 1
print(' '.join(("You win", player, "beats", computer)))
end_game = input("To exit enter N, to play again enter any key ")
if end_game == 'n' or end_game == 'N':
print("THANKS FOR PLAYING " + user_name + '!')
break
total_guess = 0

Python - "Local x variable referenced before assignment" (CLOSED)

I'm having a problem with my python program and I've spent too many time trying to fix it but I can't. I was hoping you could help me.
Anyway, the problem is in:
def choose_winnerPvsP(p1,p2):
When I run it the part of Player vs Computer woks perfectly, but the part of Player vs Player gives me this error: "Local variable player_1score referenced before assignment"
I can't understand why the if's are never checked.
PS: I'm a beginner and I know that the code could be more compact, but for now I just want this thing working. Thanks in advance
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
from random import *
from time import sleep
import sys
import os
os.system("cls") #ou "clear" se estiver em linux
print "******Welcome to Rock Paper Scissors Game!******\n"
playerOrComputer = str(raw_input("Would you like to play against a friend or a computer?(F or C) \n" ))
p1_score = 0
p2_score = 0
def player_turn(nome_player):
print "What do you want to do, " + nome_player + "?"
pchoice = str(raw_input("ROCK (R) PAPER (P) SCISSORS (S) or Quit\n>> "))
if pchoice == "R" or pchoice == "r":
print "You chosed ROCK"
elif pchoice == "P" or pchoice == "p":
print "You chosed PAPER"
elif pchoice == "S" or pchoice == "s":
print "You chosed SCISSORS"
elif pchoice == "quit" or pchoice == "Quit":
sys.exit()
else:
print "I didn't understand! Please repeat."
player_turn()
return pchoice
def player_turn2p(nome_player1,p2):
print "What do you want to do, " + nome_player1 + "?"
pchoice1 = str(raw_input("ROCK (R) PAPER (P) SCISSORS (S)\n>> "))
if pchoice1 == "R" or pchoice1 == "r":
pchoice1 = "ROCK"
print "You chosed ROCK"
sleep(1)
os.system("cls")
elif pchoice1 == "P" or pchoice1 == "p":
pchoice1 = "PAPER"
print "You chosed PAPER"
sleep(1)
os.system("cls")
elif pchoice1 == "S" or pchoice1 == "s":
pchoice1 = "SCISSORS"
print "You chosed SCISSORS"
sleep(1)
os.system("cls")
elif pchoice1 == "quit" or pchoice == "Quit":
sys.exit()
else:
print "I didn't understand!"
player_turn2p(nome_player1,p2)
print "What do you want to do, " + p2 + "?"
pchoice2 = str(raw_input("ROCK (R) PAPER (P) SCISSORS (S)\n>> "))
if pchoice2 == "R" or pchoice2 == "r":
print p2 + " chosed ROCK\n" + nome_player1 + " chosed " + pchoice1
elif pchoice2 == "P" or pchoice2 == "p":
print p2 + " chosed PAPER\n" + nome_player1 + " chosed " + pchoice1
elif pchoice2 == "S" or pchoice2 == "s":
print p2 + " chosed SCISSORS\n" + nome_player1 + " chosed " + pchoice1
elif pchoice1 == "quit" or pchoice == "Quit":
sys.exit()
else:
print "I didn't understand!"
player_turn2p(nome_player1,p2)
return pchoice1, pchoice2
def computer_turn():
choicecomp = randint(1,3)
if choicecomp == 1:
choicecomp = "ROCK"
elif choicecomp == 2:
choicecomp = "PAPER"
elif choicecomp == 3:
choicecomp = "SCISSORS"
return choicecomp
#1-Rock 2-Paper 3-Scissors
def choose_winnerPvsP(p1,p2):
player_choice1, player_choice2= player_turn2p(p1,p2)
if player_choice1 == "R" and player_choice2 == "R" or player_choice1 == "r" and player_choice2 == "r" or player_choice1 == "R" and player_choice2 == "r" or player_choice1 == "r" and player_choice2 == "R":
player_1score = "lose"
player_2score = "win"
print "******It's a draw!******"
elif player_choice1 == "R" and player_choice2 == "P" or player_choice1 == "r" and player_choice2 == "p" or player_choice1 == "R" and player_choice2 == "p" or player_choice1 == "r" and player_choice2 == "P":
player_1score = "lose"
player_2score = "win"
print "******" + p2 + " wins!******"
elif player_choice1 == "R" and player_choice2 == "S" or player_choice1 == "r" and player_choice2 == "s" or player_choice1 == "R" and player_choice2 == "s" or player_choice1 == "r" and player_choice2 == "S":
player_1score = "win"
player_2score = "lose"
print "******" + p1 + " wins!******"
elif player_choice1 == "P" and player_choice2 == "R" or player_choice1 == "p" and player_choice2 == "r" or player_choice1 == "P" and player_choice2 == "r" or player_choice1 == "p" and player_choice2 == "R":
player_1score = "win"
player_2score = "lose"
print "******" + p1 + " wins!******"
elif player_choice1 == "P" and player_choice2 == "P" or player_choice1 == "p" and player_choice2 == "p" or player_choice1 == "P" and player_choice2 == "p" or player_choice1 == "p" and player_choice2 == "P":
player_1score = "draw"
player_2score = "draw"
print "******It's a draw!******"
elif player_choice1 == "P" and player_choice2 == "S" or player_choice1 == "p" and player_choice2 == "s" or player_choice1 == "P" and player_choice2 == "s" or player_choice1 == "p" and player_choice2 == "S":
player_1score = "lose"
player_2score = "win"
print "******" + p2 + " wins!******"
elif player_choice1 == "S" and player_choice2 == "R" or player_choice1 == "s" and player_choice2 == "r" or player_choice1 == "S" and player_choice2 == "r" or player_choice1 == "s" and player_choice2 == "R":
player_1score = "lose"
player_2score = "win"
print "******" + p2 + " wins!******"
elif player_choice1 == "S" and player_choice2 == "P" or player_choice1 == "s" and player_choice2 == "p" or player_choice1 == "S" and player_choice2 == "p" or player_choice1 == "s" and player_choice2 == "P":
player_1score = "win"
player_2score = "lose"
print "******" + p1 + " wins!******"
elif player_choice1 == "S" and player_choice2 == "S" or player_choice1 == "s" and player_choice2 == "s" or player_choice1 == "S" and player_choice2 == "s" or player_choice1 == "s" and player_choice2 == "S":
player_1score = "draw"
player_2score = "draw"
print "******It's a draw!******"
return player_1score, player_2score
def choose_winnerPvsC(player_name,player_choice, computer_choice):
if player_choice == "R" and computer_choice == "ROCK" or player_choice == "r" and computer_choice == "ROCK":
computer_score = "draw"
player_score = "draw"
print "******It's a draw!****** "
elif player_choice == "R" and computer_choice == "PAPER" or player_choice == "r" and computer_choice == "PAPER":
computer_score = "win"
player_score = "lose"
print "******I win!****** "
elif player_choice == "R" and computer_choice == "SCISSORS" or player_choice == "r" and computer_choice == "SCISSORS":
player_score = "win"
computer_score = "lose"
print "******" + player_name + " wins!****** "
elif player_choice == "P" and computer_choice == "ROCK" or player_choice == "p" and computer_choice == "ROCK":
player_score = "win"
computer_score = "lose"
print "******" + player_name + " wins!****** "
elif player_choice == "P" and computer_choice == "PAPER" or player_choice == "p" and computer_choice == "PAPER":
computer_score = "draw"
player_score = "draw"
print "******It's a draw!******"
elif player_choice == "P" and computer_choice == "SCISSORS" or player_choice == "p" and computer_choice == "SCISSORS":
computer_score = "win"
player_score = "lose"
print "******I win!****** "
elif player_choice == "S" and computer_choice == "ROCK" or player_choice == "s" and computer_choice == "ROCK":
computer_score = "win"
player_score = "lose"
print "******I win!****** "
elif player_choice == "S" and computer_choice == "PAPER" or player_choice == "s" and computer_choice == "PAPER":
player_score = "win"
computer_score = "lose"
print "******" + player_name + " wins!****** "
elif player_choice == "S" and computer_choice == "SCISSORS" or player_choice == "s" and computer_choice == "SCISSORS":
computer_score = "draw"
player_score = "draw"
print "******It's a draw!****** "
return player_score, computer_score
def loopPvsP():
p1score = 0
p2score = 0
while True:
player_1score, player_2score = choose_winnerPvsP(p1,p2)
if player_1score == "win":
p1score += 1
elif player_2score == "win":
p2score += 1
print p1 + ":",p1score
print p2 + ":",p2score
if p1score == 5:
print "-+-+-+-+-"+ p1.upper() + "IS THE BIG WINNER!!-+-+-+-+-"
break
elif p2score == 5:
print "-+-+-+-+-"+ p2.upper() + "IS THE BIG WINNER!!-+-+-+-+-"
break
def loopPvsC():
pscore = 0
cscore = 0
while True:
pchoice = player_turn(p1c)
choicecomp = computer_turn()
print "I chosed "+ choicecomp + "!"
player_score, computer_score = choose_winnerPvsC(p1c,pchoice, choicecomp)
if computer_score == "win":
cscore += 1
elif player_score == "win":
pscore += 1
print "Computer:",cscore
print p1c + ":",pscore
if pscore == 5:
print "-+-+-+-+-YOU ARE THE BIG WINNER!!-+-+-+-+-"
break
elif cscore == 5:
print "-+-+-+-+-I'M THE BIG WINNER!!-+-+-+-+-"
break
if playerOrComputer == "f" or playerOrComputer == "F":
p1 = str(raw_input("Player 1: "))
p2 = str(raw_input("Player 2: "))
os.system("cls")
loopPvsP()
elif playerOrComputer == "c" or playerOrComputer == "C":
p1c = str(raw_input("Player: "))
os.system("cls")
loopPvsC()
else:
print "That's not what I asked!"
Meh. Debuggers are overrated -- I go years without using one in Python. Just add a final else clause to your big if/elif statement:
else:
print "Did not find answer"
print repr(player_choice), repr(computer_choice)
If that doesn't hit, then it means you have a typo somewhere else, perhaps. Anyway, you can certainly add more debugging statements -- one to each elif clause, if need be.
Which brings up another point -- you have a lot of if/elif clauses.
You might want to investigate things like dicts with values you can add together, or functions you can call, to reduce the number of if/else statements you need.

Categories