0

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.')
Thomas Fritsch
  • 9,639
  • 33
  • 37
  • 49
negru08
  • 5
  • 1
  • 4
  • You mean you want it to skip the 3 prints at the bottom of the loop? – Carcigenicate Mar 16 '18 at 18:47
  • No, but I am wanting it to get to that section only after the user has entered 1 for the second input. I totally just released that those print statements were part of the loop.I'll think on that now that I see that. – negru08 Mar 16 '18 at 18:49

1 Answers1

1

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.

Christian Dean
  • 22,138
  • 7
  • 54
  • 87
  • Yea thats what I was trying to go for but by bringing my print statements out I loose my else statement when user enters an invalid number for rolls in the beginning. I guess I would also need to figure out how to keep the user from entering a non positive number for my second input also. Hmm. Thank you. That has gotten me one step further. I will think about it for a bit. – negru08 Mar 16 '18 at 19:05
  • Actually @negru08, `else` wasn't _really_ testing if a user's input was invalid. It only seemed that way. See the other question I linked to in my answer if you want to validate your user input. I'm sure you'll figure it out. If you really need me to though, ping me back and I'll add an example of validating the user's input to my answer. – Christian Dean Mar 16 '18 at 19:08
  • Well I added an if statement after my first input that basically checks that num_rolls is greater that 1 or num_rolls == str and if not then it print a statement saying that is an invalid roll....but I realized that Python cant compare an int against a string. So how can I make it to where the user cant enter a negative number or a letter? – negru08 Mar 16 '18 at 19:31
  • Thank you. That works but I just dont know what you are doing lol. I am very intro to python lol. I havent learned def or return yet. Or what rolls.isdigit command . – negru08 Mar 16 '18 at 20:52
  • Glad I could help out @negru08! Since you're new to Python, I suggest taking the [official Python tutorial](https://docs.python.org/3/tutorial/index.html) to learn more. – Christian Dean Mar 16 '18 at 21:06
  • Thank you. I am taking a intro to python class but the problem is we are only on chapter 5 which hasn't covered those yet but he is wanting us to do what I am trying to do above and I cant figure it out with what I have learned so far. He is on vacation for a week so he isnt responding. But from all my googling I cant find a simple way to verify that the user only enters an integer and not break code when he enters a character without using try or except or anything else I haven't learned yet lol. But thank you. This has been informative. – negru08 Mar 16 '18 at 21:25