0
\$\begingroup\$

I am trying to make a dice rolling game. I want to generate a random integer in the range of the number of dice I have multiplied by the number of sides the dice has. For example, if two dice were played with three sides, then there could be any number between 2 and 6.

Here is my code; when I put in, for example one dice and two sides, it prints numbers upwards of 13. The problem is that it doesn't create an integer between the amount of sides and dice.

condition = "Start" 
while condition == "Start": 
Dice = int(input("Please select number of dice you would like to use (atleast 1)"))
Sides = int(input("Please select number of sides on dice you would like to use (atleast 2)"))
if Dice <= 0 and Sides <= 1:
    condition = "Start"
elif Dice >= 1 and Sides >= 2:
    print("You have selected", Dice," dice and", Sides,"Sides")
    condition = "play" 
    Roll1 = 0
    Roll2 = 0 
    Score1 = 0 
    Score2 = 0
while condition == "play": 
        Roll1 += randint(Dice,Sides)
        Roll2 += randint(Dice,Sides)
        print (name1,"'s Roll", Roll1) 
        print (name2,"'s Roll", Roll2)
\$\endgroup\$
  • \$\begingroup\$ What's the issue you're facing? \$\endgroup\$ – Alexandre Vaillancourt May 1 '17 at 23:56
  • \$\begingroup\$ it doesn't create an integer between the amount of sides and dice \$\endgroup\$ – Carlos Omeck May 1 '17 at 23:58
  • \$\begingroup\$ or gives random values for example if i put one dice and 2 sides it would go up to 5 and 6 whereas it should only go upto 2 at most for a 2 sided dice \$\endgroup\$ – Carlos Omeck May 1 '17 at 23:59
  • \$\begingroup\$ This is a general programming question and has nothing to do with gamedev \$\endgroup\$ – Bálint May 2 '17 at 7:46
  • \$\begingroup\$ @Bálint, it is a general programming question being specifically applied to game developing. I brought this up on meta, a while back. It is very much on topic. At the very least, it is very much contested. \$\endgroup\$ – Gnemlock May 3 '17 at 1:47
2
\$\begingroup\$

You seem to be missing a loop to simulate the number of dice there are to roll, and you seem to be using the randint function incorrectly.

I haven't tested this code but it should get you started:

condition = "Start" 
while condition == "Start": 
    Dice = int(input("Please select number of dice you would like to use (atleast 1)")) 
    Sides = int(input("Please select number of sides on dice you would like to use (atleast 2)")) 
    if Dice <= 0 and Sides <= 1:
        condition = "Start"
    elif Dice >= 1 and Sides >= 2:
        print("You have selected", Dice," dice and", Sides,"Sides")
        score = 0
        for i in range(Dice):
            roll = randint(1,Sides)
            score += roll
            print ("Die # ", i + 1, "'s Roll :", roll)
        print ("Score: ", score)

Basically, the for loop there will iterate over the number of dice roll that the user expected. With that, you don't need to create a variable for each roll.

And the randint method needs the min and max values. In the code you provided, you are mixing apples and oranges; Dice and Sides are not really related. What you want is the minimum value of one (the minimum value will be one for any dice), and the max value, as you could see, is the Sides variable, as you used.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.