0

I have a code that rolls two dice, for each players where they type 'roll' to roll each of their dice. However, if they don't type in 'roll' correctly, the code doesn't work properly and instead of asking the user over and over again to enter 'roll' again/ correctly, it goes through the code regardless without validating their input.

The code is supposed to ask Player 1 to roll their first then second dice for Round 1, then move onto Player 2's two dice for round one, then round 2, until both player have gone through 5 rounds, and if they type it incorrectly, it just asks for the right input until it's right.

import random
tot1 = 0
tot2 = 0
tot2 = 0
rnd2 = 0

for i in range (1,6):
while True:
from random import randint
    print()
    print("Player 1")
    ro1_1 = input("Type 'roll' to roll your 1st dice: ")
    if ro1_1 == 'roll':
        dice1_1 = (randint(1,6))
        print("Player1 dice 1:", dice1_1)
    else:
        ro1_1 = input("Type 'roll' to roll your 1st dice: ")
    ro1_2 = input("Type 'roll' to roll your 2nd dice: ")
    if ro1_2 == "roll":
        dice1_2 = (randint(1,6))
        print("Player1 Dice 2:", dice1_2)
    else:
        ro1_2 = input("Type 'roll' to roll your 1st dice: ")
    print()
    print ("Player1's total for round",(rnd1)," is:",tot1) 
    print()
    print ("Player 2")
    ro2_1 = input("Type 'roll' to roll your 1st dice: ")
    if ro2_1 == 'roll':
        dice2_1 = (randint(1,6))
        print("Player2 Dice 1:", dice2_1)
    else:
        ro1_1 = input("Type 'roll' to roll your 1st dice: ")
    ro2_2 = input("Type 'roll' to roll your 2nd dice: ")
    if ro2_2 == 'roll':
        dice2_2 = (randint(1,6))
        print("Player2 Dice 2:", dice2_2)
    else:
        ro2_2 = input("Type 'roll' to roll your 1st dice: ")
        break
    print()

print ("Player2's total for round",(rnd2)," is:",tot2)
    print()
    break
STRhythm
  • 113
  • 8
  • Possible duplicate of [Asking the user for input until they give a valid response](https://stackoverflow.com/questions/23294658/asking-the-user-for-input-until-they-give-a-valid-response) – Matthias Dec 13 '18 at 10:04

1 Answers1

0

First, move from random import randint to the top - at least outside the while loop. This won't solve the problem, but just saying.

Next, you want something to stop until the player types "roll". In several places.

Write a function:

def wait_for_right_input():
    while True:
        if input("Type 'roll' to roll your 1st dice: ") == 'roll':
            break

You can now call this where needed:

from random import randint

for i in range (1,6):
#while True: ## not sure why you have both, and this would make the indents wrong
    print()
    print("Player 1")

    wait_for_right_input() #<-- here
    dice1_1 = randint(1,6)
    print("Player1 dice 1:", dice1_1)

    wait_for_right_input() #<-- here
    dice1_2 = randint(1,6)
    print("Player1 Dice 2:", dice1_2)

    # etc

If you want to loop until invalid input (I presume the reason for the while True and break) you can change the function to return a Boolean indicating whether to continue or not.

doctorlove
  • 18,872
  • 2
  • 46
  • 62