0

This is what the instructions are telling me to do... main – This function is the main routine. It should do the following: Ask the user how many dice they want to roll. If the user enters a number less than 1 or greater than 10, the program should continue to ask the user for a valid number between 1 and 10. Remember you can use a while loop to do input validation. Once the program has a valid number from the user, it should use that number as an argument when calling the roll_dice function.

roll_dice – This function has one parameter, num_dice, which is the number of dice to roll. Since the program will be displaying the total of the dice, start by initializing a variable to 0 which will keep a running total. Use a for loop to roll each die the number of times specified. Use the randint function from the random module to get a random digit between 1 and 6 inclusive. Print the number of the loop iteration and the die value as indicated in the Sample Output. Make sure to update the running total INSIDE the for loop. After the for loop is complete, print the total value of all the dice that were rolled.

And this is the code I have so far:

import random


def main():
    num_dice = int(input('How many dice do you want to roll?'))
    while num_dice < 1 or num_dice > 10:
        print('Enter a number between 1 and 10.')
        num_dice = (input('How many dice do you want to roll?'))
        roll_dice(num_dice)
def roll_dice(num_dice):
    rolls = 0
    for i in range(num_dice):
        print(f'Roll #', rolls, random.randint(1, 6))
        rolls+=1


main()
Forgetten
  • 3
  • 1
  • 4
  • what is your question? – bunyaminkirmizi Feb 05 '22 at 03:16
  • I kept getting an error and can't figure it out, I added the error message, I am basically asking how I can get the code working – Forgetten Feb 05 '22 at 03:21
  • 1
    convert your second input as int `num_dice = int(input('How many dice do you want to roll?'))` – Fareed Khan Feb 05 '22 at 03:22
  • Tou converted the input to an `int` the first time, but not the second. Your main problem is that you do `rolls += 1`, but you've been asked to return the total of rolls, so you should add the outcome of `random.randint(1, 6)` to it, as well as print it (several ways to do it) – Grismar Feb 05 '22 at 03:23
  • Thank you, I didn't realize that I missed that – Forgetten Feb 05 '22 at 03:26

4 Answers4

1

There's a few improvements:

  • you typically don't want to repeat code, so it would be better to only have the input statement in there once (that would have also avoided the mistake you made on the second one);
  • you are asked to return a sum total of rolls, but also to print each roll; you can either compute it before printing, or print the assigned value directly (with a walrus operator :=)
  • once you have the total, you'll need to do something with it, like print it.

Something like:

import random


def main():
    # you can loop 'forever' and then break out under the right condition
    while True:
        # you'd forgotten one of the int()
        num_dice = int(input('How many dice do you want to roll?'))
        if num_dice < 1 or num_dice > 10:
            print('Enter a number between 1 and 10.')
        else:
            break
    result = roll_dice(num_dice)
    print(f'The total for {num_dice} rolls was {result}.')


def roll_dice(num_dice):
    rolls = 0
    for i in range(1, num_dice + 1):
        # you printed rolls, but you want i, starting at 1
        # roll := something, causes roll to be assigned, but also returns the value
        print(f'Roll #{i} = {(roll := random.randint(1, 6))}')
        rolls += roll
    return rolls


main()

(Edit: I noticed that you were already using an f-string in roll_dice, so you may as well make actual use of it)

I like @Blckknght's suggestion for another nice use of the walrus operator in a shorter version of main():

def main():
    # you can loop 'forever' and then break out under the right condition
    while (n := int(input('How many dice do you want to roll?'))) < 1 or n > 10:
        print('Enter a number between 1 and 10.')
    result = roll_dice(n)
    print(f'The total for {n} rolls was {result}.')

Beware though: if someone asks you to explain your code, you better know what's going on here:

  • due to short-circuit evaluation, the first part of the or in the while is executed first, and the second part is only executed if the first part is False; that's why you can use the := in the first part and know that num_dice will have been updated for the second part;
  • the walrus operator assigns the righthand value, but also returns it, so it can be compared to 1
Grismar
  • 27,561
  • 4
  • 31
  • 54
  • 1
    Since you're already using assignment expressions in the `roll_dice` function, you might consider using one in `main` too, to take care of the `while` loop repeating itself: `while (num_dice := int(input(prompt))) < 1 or num_dice > 10: ...` – Blckknght Feb 05 '22 at 03:47
0
 def main():
     num_dice = int(input('How many dice do you want to roll?'))
     while num_dice < 1 or num_dice > 10:
     print('Enter a number between 1 and 10.')
     num_dice = (input('How many dice do you want to roll?'))
     roll_dice(num_dice)
 def roll_dice(num_dice):
     rolls = 0
     for i in range(num_dice):
        print(f'Roll #', rolls, random.randint(1, 6))
        out_ran_num =   random.randint(1, 6)
        rolls+=out_ran_num

     return rolls

main()

Please check indentation

  • Wasn't the indentation in the original post for the main correct? The goal is to wait for valid input, so the input would go in the while loop (and the print as well) but not the actual roll function. – Nicholas Zolton Feb 05 '22 at 03:40
  • You missed the main part, the int casting.The line `num_dice = (input('How many dice do you want to roll?'))` should actually be `num_dice = int(input('How many dice do you want to roll?'))` – Anshumaan Mishra Feb 05 '22 at 03:41
  • Yep, didn’t see I missed that – Chaitu Boggavarapu Feb 05 '22 at 04:01
  • 1
    @Nicholas Zolton Yes the indentation in the file was correct, but when I pasted the code there might be some spaces added. So I was just giving a hint ahead – Chaitu Boggavarapu Feb 05 '22 at 04:04
  • I understand! It just might be worth updating the post since Python relies so heavily on indentation! – Nicholas Zolton Feb 05 '22 at 22:29
0

First, we want to keep asking the user how many dice they want to roll until they give a valid input, we can do that with a while loop like so:

valid_integer = False

# While the user hasn't given a valid integer
while valid_integer == False:

    # Note: this will crash if the user does not input a number
    num_dice = int(input('How many dice do you want to roll?'))

    # Checking if the input is valid
    if num_dice < 1 or num_dice > 10:
        print("Please enter a number between 1 and 10")
        # Because we did not change the variable valid_integer, it will loop again
    else:
        valid_integer = True
        print(roll_dice(num_dice))

Now we need to create the roll_dice function, it needs to take one parameter (the number of dice to roll) and return a integer (the total of the dice rolls)

import random

# num_dice: int is known as type hinting, it is used to indicate the type of a value
def roll_dice(num_dice: int):
    total = 0

    for i in range(num_dice):
        roll = random.randint(1, 6)
        total += roll

    return total

Now we can put the code together! We need to put the loop in a function main() and call it. Don't forget your imports!

import random

def main():
    valid_integer = False

    # While the user hasn't given a valid integer
    while valid_integer == False:

        # Note: this will crash if the user does not input a number
        num_dice = int(input('How many dice do you want to roll?'))

        # Checking if the input is valid
        if num_dice < 1 or num_dice > 10:
            print("Please enter a number between 1 and 10")
            # Because we did not change the variable valid_integer, it will loop again
        else:
            valid_integer = True
            print(roll_dice(num_dice))

# num_dice: int is known as type hinting, it is used to indicate the type of a value
def roll_dice(num_dice: int):
    total = 0

    for i in range(num_dice):
        roll = random.randint(1, 6)
        total += roll

    return total

if __name__ == "__main__":
    main()

more info on if __name__ == "__main__" What does if __name__ == "__main__": do?

Dharman
  • 30,962
  • 25
  • 85
  • 135
s1072489
  • 1
  • 1
0
  1. In main function in while loop there should int for taking input and should use roll_dice function outside while loop. there should be return word to get total as shown in figure
  2. In roll_dies function there should should be rolls+=random.randint(1,6) and also return rolls outside for loop

enter image description here

Avinash
  • 359
  • 3
  • 5