dimanche 14 avril 2019

Trying to get chunk of python code to run a certain number of times

I have a rock paper scissors game programmed and I have some win counters with it. I would like the game to repeat until one of the win counters has hit 4 but can't figure out a good way to do it.

I tried using a while loop but couldn't get it to stop repeating over and over and couldn't find a way to stop it.


import random
import time

moves=["rock", "paper", "scissors"]

play_move=str(input("Please choose rock, paper, or scissors: "))

computer_move= moves[random.randint(0,2)]

print("Ready \n")
time.sleep(1)
print("Set \n")
time.sleep(1)
print("GO! \n")
time.sleep(1)

comp_wins=0

player_wins=0

ties= 0

if player_wins <= 4 or comp_wins <=4:
    if play_move == computer_move:
        print("Both players chose", play_move, "and there is a tie, please play again")
        ties= ties+1
    elif play_move == "rock":
        if computer_move == "paper":
            print(computer_move, "beats", play_move, "You have lost!")
            comp_wins= comp_wins+1
        else:
            print(play_move, "beats", computer_move, "You have won!")
            player_wins=player_wins+1
    elif play_move == "paper":
        if computer_move == "scissors":
            print(computer_move, "beats", play_move, "You have lost!")
            comp_wins= comp_wins+1
        else:
            print(play_move, "beats", computer_move, "You have won!")
            player_wins=player_wins+1
    elif play_move == "scissors":
        if computer_move == "rock":
            print(computer_move, "beats", play_move, "You have lost!")
            comp_wins= comp_wins+1
        else:
            print(play_move, "beats", computer_move, "You have won!")
            player_wins=player_wins+1
    else:
        print("Your selection was invalid please try again")


Aucun commentaire:

Enregistrer un commentaire