1

I am a complete newbie and have tried solving this problem (with my own head and with online research) for the last 5 hours.

Below is a snippet of a function we have written to simulate a game. We want to offer the ooportunity to start a new round - meaning if a player hits "b", the game should start again at the beginning of the range (0, players). But right now it just goes onto the next player in the range (if player 1 enters "b", the program calls player 2)

players = input(4)
if players in range(3, 9):
    for player in range(0, players):
        sum_points = 0
        throw_per_player_counter = 0
        print("\nIt is player no.", player+1, "'s turn!\n")
        print("\nPress 'return' to roll the dice.\n"
              "To start a new round press 'b'.\n"
              "Player", player+1)
        roll_dice = input(">>> ")
        if roll_dice == "b":
            player = 0
            throw_per_player_counter = 0
            sum_points = 0
            print("\n * A new round was started. * \n")

I have tried return and break, also tried to put it all in another while-loop... failed. Break and return just ended the function. Any hints highly appreciated!

1 Answer 1

1

you could change the for loop to a while loop. instead of using a range, make player a counter

players = 4
if 3 <= players < 9:  
    player = 0  # here's where you make your counter
    while player < players:
        sum_points = 0
        throw_per_player_counter = 0
        print("\nIt is player no.", player+1, "'s turn!\n")
        print("\nPress 'return' to roll the dice.\n"
              "To start a new round press 'b'.\n"
              "Player", player+1)
        roll_dice = input(">>> ")
        player += 1  # increment it
        if roll_dice == "b":
            player = 0  # now your reset should work
            throw_per_player_counter = 0
            sum_points = 0
            print("\n * A new round was started. * \n")
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you! that did indeed help to break the loop! Some test cases fail with the sum_points (it's apparently doubling up points it shouldn't), and it sometimes miscalculates which players turn it is. Will work on that :-)
i noticed a lot of player + 1 here and that could easily be a cause for errors and miscalculations. the variable does not represent the actual value. since player represents a player number that starts with 1, it might be worth re-writing to make the player variable reflect its actual value consistently. player = 1, while player <= players:, print("Player", player). this might make your bug (wherever it's hiding) easier to spot. also, glad it helped.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.