0

I am trying to restart this program if what the user gets from picking the two numbers from the dice roll is already gone from the overall list. It will ask them to roll again and everything that just happened will go back like they never clicked 'e'.

roll = input("Player turn, enter 'e' to roll : ")

    dice_lst = []
    if roll == "e":
        for e in range(num_dice):
            d_lst = random.randint(1,6)
            dice_lst.append(d_lst)
    else:
        print("Invalid ----- Please enter 'e' to roll the dice")
        # Add in the invalid input error



    print("")
    print("You rolled", dice_lst)
    dice_choose = int(input("Choose a dice : "))
    dice_lst.remove(dice_choose)
    print("")
    print("You are left with", dice_lst)
    dice_choose1 = int(input("Choose a dice : "))
    dice_lst.remove(dice_choose1)
    print("")


    while True:
        sum1 = dice_choose + dice_choose1
        if sum1 in lst_player:
            break
        else:

            print("You have made an invalid choice")


    sum1 = dice_choose + dice_choose1
    print(dice_choose, "+", dice_choose1, "sums to", sum1)
    print(sum1, "will be removed")
    print("")

    lst_player.remove(sum1)
0

1 Answer 1

1

If you want to repeat code until some conditions are met, you can use a while True loop or while <condition> loop, using continue (continue to the next iteration) to "reset" when some invalid situation is reached:

while True:
    roll = input("Player turn, enter 'e' to roll : ")

    dice_lst = []
    if roll == "e":
        for e in range(num_dice):
            d_lst = random.randint(1,6)
            dice_lst.append(d_lst)
    else:
        print("Invalid input")
        continue # Go back to asking the player to roll

    print("")
    print("You rolled", dice_lst)
    dice_choose = int(input("Choose a dice : "))
    dice_lst.remove(dice_choose)
    print("")
    print("You are left with", dice_lst)
    dice_choose1 = int(input("Choose a dice : "))
    dice_lst.remove(dice_choose1)
    print("")

    sum1 = dice_choose + dice_choose1
    if sum1 not in lst_player:
        print("You have made an invalid choice")
        continue # Go back to asking the player to roll

    print(dice_choose, "+", dice_choose1, "sums to", sum1)
    print(sum1, "will be removed")
    print("")

    lst_player.remove(sum1)
    break # Break out of the loop

I'm not entirely sure how this code is intended to work, but you may need to move blocks around and/or reset dice before continue.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, this helps a lot.

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.