kyle lee
kyle lee

Reputation: 23

Python Rock Paper Scissors game ( Loop for certain condition )

I made a simple Rock Paper Scissors program, and I need to add a certain condition to this program.. I have to Let the user play continuously until either the user or the computer wins more than two times in a row.I tried to find the answer in and out but unfortunately couldn't find it..

First off I tried

 gameOver = False
 playerScore = 0
 computerScore = 0

and added

while not gameOver:
    main()
    if playerScore == 2 :
        gameOver = True

and also added playerScore += 1 to the if statements..
But wouldn't work ...
any advise would help and much appreciated in advance.. cheers!

And here is my code..

import random
import sys


def main():
    player = input("Enter your choice in number (rock 1 / paper 2 / scissors 0) :")


    if (player == 0):
        player = "scissors"
    elif (player == 1):
        player = "rock"
    elif (player == 2):
        player = "paper"
    else:
    print("Invalid Input Quitting...")
    sys.exit(0)


    computer = random.randint(0,2)
    if (computer == 0):
        computer = "scissors"
    elif (computer == 1):
        computer = "rock"
    elif (computer == 2):
        computer = "paper"





    if (player == computer):
        print("Player is ",player, "Computer is ",computer," You Draw!")

    elif (player == "rock"):
        if (computer == "paper"):
            print("Player is ",player, "Computer is ",computer," You Lost!")


        else:
            print("Player is ",player, "Computer is ",computer," You Win!")



    elif (player == "paper"):
        if (computer == "rock"):
            print("Player is ",player, "Computer is ",computer," You Win!")


        else:
            print("Player is ",player, "Computer is ",computer," You Lost!")


    elif (player == "scissors"):
        if (computer == "rock"):
            print("Player is ",player, "Computer is ",computer," You Lost!")


        else:
            print("Player is ",player, "Computer is ",computer," You Win!")

Upvotes: 0

Views: 1758

Answers (3)

Eric Hughes
Eric Hughes

Reputation: 841

You may be getting an error when attempting to modify the globals, but from your example it's not entirely clear. If you try to modify playerScore or computerScore in your main() method, it'll yell at you unless you have a statement like:

global computerScore

before you modify it.

Also, avoid repeating yourself in your code. I was able to trim much of your code out by using the following:

computerWins = False

print "Player is %s; Computer is %s" % (computer, player)

if (player == computer):
  print "Draw"
  return 0
elif (player == "rock"):
  computerWins = computer == "paper"
elif (player == "paper"):
  computerWins = computer == "scissors"
elif (player == "scissors"):
  computerWins = computer == "rock"

if computerWins:
  global computerScore
  computerScore = computerScore + 1
  print "Computer wins"
else:
  global playerScore
  playerScore = playerScore + 1
  print "You win"

Upvotes: 0

Antonio E.
Antonio E.

Reputation: 359

I think you are asking only a schema: (this is no true code)

Program starts:
gameover = False
lastWinner = ""
Loop until gameover == True
    ask for player answer
    make the random choice of the computer
    winner = "asigned winner"
    if lastWinner == winner:
        gameover = True
        Print something cool about who is the winner
    else:
        lastWinner = winner

Upvotes: 0

jedwards
jedwards

Reputation: 30210

If you have your main() function return a value corresponding to who won, you could do:

gameOver = False
playerScore = 0
computerScore = 0

while not gameOver:
    player_wins = main()
    if player_wins == True:
        playerScore += 1
        computerScore = 0
    if player_wins == False:
        playerScore = 0
        computerScore += 1
    if player_wins == None:
        # Draw, do nothing to the scores
        pass
    if playerScore == 2 or computerScore == 2:
        print("Game over")
        print("  playerScore:", playerScore)
        print("  computerScore:", computerScore)
        gameOver = True

Note that I had it return True if the player won, False if the computer won, and None if it was a draw.

Upvotes: 1

Related Questions