Zii Donato
Zii Donato

Reputation: 25

Why does my rock paper scissors game not start the while loop?

So this is my code so far, trying to make a rock paper scissors game that's best out of 3.

from random import randint
print("Rock...")
print("Paper...")
print("Scissors...")

player_wins = 0
computer_wins = 0

while player_wins and computer wins < 3:  
    player1 = input("Player 1, make your move: ").lower()
    computer_choices = ["rock", "paper", "scissors"]
    computer_choice = randint(0, 2)
    computer = str[computer_choices[computer_choice]]
    print(computer)
...(then all the choice logic goes here, I add 1 to the variables of the one who wins)

if player_wins == 3:
    print("Player wins the game!")
if computer_wins == 3:
    print("Computer wins the game!")

The code never enters the while loop, how come? I really have no idea what's going on here.

Edit: I did while (player_wins and computer_wins) < 3: and now it enters the loop but doesn't exit.

Upvotes: 0

Views: 64

Answers (4)

lfeuu
lfeuu

Reputation: 26

The loop doesn't start because when it reaches the while condition at first the variable player_wins has a value of 0 and 0 is automatically converted to False to evaluate the condition, and so your initial condition is equals to False and the program never gets into the loop. You can imagine the evaluation process like so:

while player_wins and computer wins < 3

while 0 and 0 < 3

while False and True

while False # It doesn't get into the loop

More info here: http://anh.cs.luc.edu/python/hands-on/3.1/handsonHtml/boolean.html

Upvotes: 0

Goldwave
Goldwave

Reputation: 599

It's because of this line while player_wins and computer wins < 3: You are checking if player_wins is true(>0) and if computer wins is under 3. "computer wins" should be "computer_wins"

It should look like this: while player_wins < 3 and computer_wins < 3:

Upvotes: 0

E.J. Brennan
E.J. Brennan

Reputation: 46859

you probably want:

while (player_wins < 3) and (computer wins < 3):  

it looks like you are trying to say if both player_wins AND computer_wins is < 3, but your syntax is not quite right.

Upvotes: 1

andrbrue
andrbrue

Reputation: 731

player_wins is 0 when entering the loop. Did you mean

while player_wins < 3 and computer_wins < 3:

?

Upvotes: 0

Related Questions