Home > front end >  How to make the loop continue even after a part of it breaks in python
How to make the loop continue even after a part of it breaks in python

Time:01-18

The code below ends after a player has reached a value of 20. What I tried to code (but failed) was that if a player (say player 1) reaches 20 it stops that player's part of code and keeps the other player's (player 2) code running until he reaches 20 aswell and then displays who reached 20 first.

import random

player1 = 0
rollnum_player1 = 0       
player2 = 0   
rollnum_player2 = 0        

def die():
    roll = random.randint(1,6)
    return roll



while True:
    
    # Player 1 code
    player1  = die()
    print("Player 1 roll:",player1)
    rollnum_player1  = 1
    if player1 >= 20:
        print("Congratulations player 1 you have won!")
        break

    # Player 2 code
    player2  = die()
    print(player2)
    rollnum_player2  = 1
    if player2 >= 20:
        print("Congratulations player 2 you have won!")
        break

CodePudding user response:

Instead of breaking when a player wins, use if statements to skip over the code for the player who has already won. Break out of the loop when both have reached 20.

while player1 < 20 or player2 < 20:
    if player1 < 20:
        # Player 1 code
        player1  = die()
        print("Player 1 roll:",player1)
        rollnum_player1  = 1

    if player2 < 20:
        # Player 2 code
        player2  = die()
        print("Player 2 roll:",player2)
        rollnum_player2  = 1

if rollnum_player1 < rollnum_player2:
    print("Congratulations player 1 you have won!")
elif rollnum_player2 < rollnum_player1:
    print("Congratulations player 2 you have won!")
else:
    print("It's a tie!")

CodePudding user response:

I prefer you to use this code:

import random

player1 = 0
rollnum_player1 = 0
player1_check = True       
player2 = 0   
rollnum_player2 = 0
player2_check = True

winner = 0

def die():
    roll = random.randint(1,6)
    return roll


while player1_check or player2_check:
    
    # Player 1 code
    if player1_check:
        player1  = die()
        print("Player 1 roll:",player1)
        rollnum_player1  = 1
        if player1 >= 20:
            winner  = 1
            player1_win = winner
            print("Congratulations player 1 you have won!")
            player1_check = False

    # Player 2 code
    if player2_check:
        player2  = die()
        print(player2)
        rollnum_player2  = 1
        if player2 >= 20:
            winner  = 1
            player2_win = winner
            print("Congratulations player 2 you have won!")
            player2_check = False

if player1_win > player2_win:
    print('Winner is player 2')
else:
    print('Winner is player 1')

CodePudding user response:

Based on your code, this does what you asked, using only basic python. I didn't correct what i suppose are errors or typos :

import random
player1 = 0
rollnum_player1 = 0
player2 = 0   
rollnum_player2 = 0
skip_player = 0

def die():
    roll = random.randint(1,6)
    return roll



while True:
    if skip_player != 1:
        # Player 1 code
        player1  = die()
        print("Player 1 roll:",player1)
        rollnum_player1  = 1
        if player1 >= 20:
            if skip_player == 2:
                break
            print("Congratulations player 1 you have won!")
            skip_player = 1
    if skip_player != 2:
        # Player 2 code
        player2  = die()
        print(player2)
        rollnum_player2  = 1
        if player2 >= 20:
            if skip_player == 1:
                break
            skip_player = 2
            print("Congratulations player 2 you have won!")

The idea is that when somebody reaches 20, you check if the other player won, and if so, you break the loop. Else you add set skip_player to his player numlber, so he doesn't play anymore, and when the other player reaches 20, the game stops. Before makign a player play, you check skip_player to ensure he didn't already win.

  • Related