0

In the following program for rock paper scissors something is wrong with my loop. Using the input for set length, the game is played N times for each set. For cases in which results are player 2, computer 0; player 0, computer 2; player 2, computer 5; player 0, computer 4; an extra game is added to the set. I have changed the function many times and can't figure out what is wrong.

def rpsls_play():
    print("Welcome to the Rock-Scissors-Paper-Lizard-Spock game!")
    player_sets=0
    N=int(input("Select set length: "))
    times=0
    player_wins=0
    computer_wins=0
    while times < N:
        times +=1
        print("Now beginning game", times)
        if rpsls_game()==1:
            player_wins +=1
        else:
            computer_wins +=1
        print("Set score: Player", str(player_wins)+", Computer", str(computer_wins))
    else:
        pass

    if player_wins==computer_wins:
        while abs(player_wins-computer_wins)<2:         
            times +=1
            print("Now beginning game", times)
            if rpsls_game()==1:
                player_wins +=1
            else:
                computer_wins +=1
            print("Set score: Player", str(player_wins)+", Computer", str(computer_wins))
    else:
        pass

    if player_wins>computer_wins:
        print("Congratulations! You have won in", times, "games.")
        player_sets +=1
    elif computer_wins>player_wins:
        print("Too bad! You have lost in", times, "games.")

    pass

thanks for your help

user2906979
  • 307
  • 2
  • 3
  • 11

2 Answers2

1

I can't check your code because there is no function rpsls_game().

You didn't say what wrong in your game - what do you get and what do you expect ?

I can only guessing that you have problem with line

if player_wins==computer_wins:

Extra game is added after N games only if player_wins == computer_wins.

You don't need that line.

furas
  • 134,197
  • 12
  • 106
  • 148
  • I needed that line because if there is a tie they must play until one is winning by two- the problem is that for some reason games are being added when there is no tie – user2906979 Oct 28 '13 at 21:20
  • English is not my native language and I thought you need extra games not only if `player_wins==computer_wins` but also if `abs(player_wins-computer_wins)<2`. I make `rpsls_game()` with `random` to test your code (python 2.x, 3.x) and I don't see problem. It works for me. It adds extra games only if `player_wins==computer_wins` – furas Oct 28 '13 at 23:27
  • Maybe you make modification in one file but accidentally you run different file with old code. – furas Oct 28 '13 at 23:38
  • thanks for the advice, i'll make sure I'm using the right file – user2906979 Oct 29 '13 at 10:22
1

Couple code design things that will help improve the code before I get to the answer:

  1. You can remove those else: pass lines.

  2. You can remove that final pass statement.

You need to change it to:

if player_wins == computer_wins:         
    times +=1
    print("Now beginning tie-breaker game. game", times)
    if rpsls_game()==1:
        player_wins +=1
    else:
        computer_wins +=1
    print("Set score: Player", str(player_wins)+", Computer", str(computer_wins))
andrewgrz
  • 435
  • 3
  • 6