Search code examples
pythonpython-3.xdice

Why won't my second input loop back up to start of loop?


So I am trying to create code that takes the user input for num_rolls and then rolls two dice that many times.

It logging how many 6's and 7's show up . It then prints out each roll and what the numbers were. I am having the code keep asking for an input for rolls until he chooses 1 which exits the loops and prints the statistics afterwards.

The problem I have is after my second input request is asked, it just continues down the code.

How can I get it to go back up and start with the while loop? Is my second input in the wrong location or do I need to create an additional loop?

Code is below:

import random

num_sixes = 0
num_sevens = 0
num_rolls = int(input('Enter number of rolls:\n'))

while num_rolls >= 1:
    for i in range(1,num_rolls + 1):
        die1 = random.randint(1,6)
        die2 = random.randint(1,6)
        roll_total = die1 + die2

        #Count number of 6 and 7 rolled
        if roll_total == 6:
            num_sixes = num_sixes + 1
        if roll_total == 7:
            num_sevens = num_sevens + 1
        if i in range(1,num_rolls+1):
             print('Roll %d is %d (%d + %d)' % (i, roll_total, die1, die2))
    num_rolls = int(input('Enter number of rolls:\n'))

    print('\nDice roll statistics:')
    print('6s:', num_sixes)
    print('7s:', num_sevens)
else:
    print('Invalid number of r2olls. Try again.')

Solution

  • I am having the code keep asking for an input forrolls in till he chooses 1 which exits the loops and prints the statistics afterwards.

    1. If you want to exit the loop when num_rolls is equal to 1, then you need to test while num_rolls > 1 rather than while num_rolls >= 1.

    2. If you only want to print the results of the dice rolling at the end of the program, then you should remove

      print('\nDice roll statistics:')
      print('6s:', num_sixes)
      print('7s:', num_sevens)
      

      From your while loop, and put it at the end of the program.

    3. Do you understand what the else clause at the end of your while loop does? I'm pretty sure the behavior is produces is not what you want. If you want to verify that the user's input is valid, that needs to be done separately from the main while loop.

      Since you didn't specify how the user's input should be validate, I'll leave that task up to you. It's quite simple, actually. See Asking the user for input until they give a valid response for more details. The most important point to note, is that you'll need a separate loop.

    Here is your code with the above modifcations made:

    import random
    
    num_sixes = 0
    num_sevens = 0
    num_rolls = int(input('Enter number of rolls:\n'))
    
    while num_rolls > 1:
        for i in range(1,num_rolls + 1):
            die1 = random.randint(1,6)
            die2 = random.randint(1,6)
            roll_total = die1 + die2
    
            #Count number of 6 and 7 rolled
            if roll_total == 6:
                num_sixes = num_sixes + 1
            if roll_total == 7:
                num_sevens = num_sevens + 1
            if i in range(1,num_rolls+1):
                 print('Roll %d is %d (%d + %d)' % (i, roll_total, die1, die2))
        num_rolls = int(input('Enter number of rolls:\n'))
    
    print('\nDice roll statistics:')
    print('6s:', num_sixes)
    print('7s:', num_sevens)
    

    When the above code is tested, it produces the output:

    Enter number of rolls:
     2
    Roll 1 is 5 (2 + 3)
    Roll 2 is 7 (1 + 6)
    Enter number of rolls:
     4
    Roll 1 is 6 (3 + 3)
    Roll 2 is 7 (5 + 2)
    Roll 3 is 12 (6 + 6)
    Roll 4 is 8 (6 + 2)
    Enter number of rolls:
     6
    Roll 1 is 7 (6 + 1)
    Roll 2 is 5 (3 + 2)
    Roll 3 is 10 (5 + 5)
    Roll 4 is 6 (1 + 5)
    Roll 5 is 11 (5 + 6)
    Roll 6 is 9 (3 + 6)
    Enter number of rolls:
     1
    
    Dice roll statistics:
    6s: 2
    7s: 3
    

    So how can I make it to where the user cant enter a negative number or a letter?

    If you want to validate that your user input is a positive, whole number, you can use the code below1:

    def get_num_rolls():
        nrolls = input('Enter number of rolls:\n')
        while not nrolls.isdigit():
            nrolls = input('Enter number of rolls:\n')
        return int(nrolls)
    

    You would then call the above function wherever you need to get user input:

    import random
    
    
    def get_num_rolls():
        nrolls = input('Enter number of rolls:\n')
        while not nrolls.isdigit():
            nrolls = input('Enter number of rolls:\n')
        return int(nrolls)
    
    
    num_sixes = 0
    num_sevens = 0
    num_rolls = get_num_rolls()
    
    
    while num_rolls > 1:
        ...
        num_rolls = get_num_rolls()
    ...
    

    1The above solution does cheat a bit by taking advantage of the fact that str.isdigit returns false for negative numbers, but it should work for your cases nonetheless.