Home > front end >  Why does my code run a while statement when the while statment is finished?
Why does my code run a while statement when the while statment is finished?

Time:04-17

I'm trying to make a rock, paper, scissors game, but each time I run it, when the game is supposed to be over, it asks the user to input rock, paper or scissors again. It doesn't add the result of that extra round into the counter, and it doesn't matter what the user input as long as the input something. I've been stuck at this and to me the entire code looks fine, but I want it to end right after either the computer or user get 3 wins. Here is the code:

import random

print("Welcome to rock, paper, scissors, where you will verse a computer, and the first one to win 3 rounds, wins!\n"
    "to chose rock, enter r, to chose paper, enter p, to enter scissors enter s.")
computer = random.choice("rps")
print(computer)

player = str(input("Rock, Paper, Scissors, Shoot! "))

player_counter = 0
computer_counter = 0

player_win = 0
computer_win = 0


while player_counter <= 2 and computer_counter <= 2:
    if player == "s" and computer == "s" or player == "p" and computer == "p" or player == "r" and computer == "r":
        print("You and the computer tied!")
        computer = random.choice("rps")
        print(computer)
        print("Your score is:", player_counter, "and the computer's score is:", computer_counter)
        player = str(input("Rock, Paper, Scissors, Shoot! "))

    elif player == "r" and computer == "p" or player == "p" and computer == "s" or player == "s" and computer == "r":
        print("computer wins!")
        computer_counter  = 1
        computer = random.choice("rps")
        print(computer)
        print("Your score is:", player_counter, "and the computer's score is:", computer_counter)
        player = str(input("Rock, Paper, Scissors, Shoot! "))

    elif player == "r" and computer == "s" or player == "p" and computer == "r" or player == "s" and computer == "p":
        print("You win!")
        player_counter  = 1
        computer = random.choice("rps")
        print(computer)
        print("Your score is:", player_counter, "and the computer's score is:", computer_counter)
        player = str(input("Rock, Paper, Scissors, Shoot! "))

    if player_counter == 3:
        print("You win the game!")
    elif computer_counter == 3:
        print("You lose the game!")

CodePudding user response:

The problem is that everything in your loop is offset -- you start a new round before the loop restarts and before the winning condition is rechecked. On the next pass through the loop, it terminates.

You can fix this bug and delete a lot of unnecessary code by just putting the choice at the start of the loop, in one spot, rather than having it copy pasted into every individual if block after the score has been updated:

import random

print(
    "Welcome to rock, paper, scissors, where you will verse a computer, "
    "and the first one to win 3 rounds, wins!\n"
    "To chose rock, enter r, to chose paper, enter p, "
    "to enter scissors enter s."
)
player_counter = 0
computer_counter = 0

while player_counter < 3 and computer_counter < 3:
    player = input("Rock, Paper, Scissors, Shoot! ")
    computer = random.choice("rps")
    print(computer)

    if player == computer:
        print("You and the computer tied!")
    elif player   computer in ("rp", "ps", "sr"):
        print("computer wins!")
        computer_counter  = 1
    elif player   computer in ("rs", "pr", "sp"):
        print("You win!")
        player_counter  = 1
    else:
        print(f"Invalid choice '{player}'.")

    print(
        f"Your score is: {player_counter} "
        f"and the computer's score is: {computer_counter}"
    )

if player_counter == 3:
    print("You win the game!")
elif computer_counter == 3:
    print("You lose the game!")

CodePudding user response:

First of all, you enter the loop 3 times and read the input 3 times, but you read the input one time before the loop here,

player = str(input("Rock, Paper, Scissors, Shoot! "))

that's why you see that unnecessary input. Secondly, you should read input only at the beginning of the loop, not inside of each condition. Here is a modified working code

import random

print("Welcome to rock, paper, scissors, where you will verse a computer, and the first one to win 3 rounds, wins!\n",
  "to chose rock, enter r, to chose paper, enter p, to enter scissors enter s.")
computer = random.choice("rps")
print(computer)

#player = str(input("Rock, Paper, Scissors, Shoot! "))

player_counter = 0
computer_counter = 0

player_win = 0
computer_win = 0


while player_counter <= 2 and computer_counter <= 2:
  player = str(input("Rock, Paper, Scissors, Shoot! "))

  if player == "s" and computer == "s" or player == "p" and computer == "p" or player == "r" and computer == "r":
      print("You and the computer tied!")
      computer = random.choice("rps")
      print(computer)
      print("Your score is:", player_counter, "and the computer's score is:", computer_counter)
      
  elif player == "r" and computer == "p" or player == "p" and computer == "s" or player == "s" and computer == "r":
      print("computer wins!")
      computer_counter  = 1
      computer = random.choice("rps")
      print(computer)
      print("Your score is:", player_counter, "and the computer's score is:", computer_counter)
     # player = str(input("Rock, Paper, Scissors, Shoot! "))

  elif player == "r" and computer == "s" or player == "p" and computer == "r" or player == "s" and computer == "p":
      print("You win!")
      player_counter  = 1
      computer = random.choice("rps")
      print(computer)
      print("Your score is:", player_counter, "and the computer's score is:", computer_counter)
      #player = str(input("Rock, Paper, Scissors, Shoot! "))
if player_counter == 3:
    print("You win the game!")
elif computer_counter == 3:
    print("You lose the game!")
  • Related