user9453645
user9453645

Reputation:

Dice roller having issues with second while loop

I'm a longterm lurker first time questioner. I'm trying to teach myself Python and while I've researched my question I couldn't find an answer. My below code runs if I remove the first While loop but currently it doesn't seem to enter the second while loop. I think I might be channelling my inner VBA which I use at work and this is only second time I've tried Python. I've tried changing the first while so it isn't just while true and tried variants on the second while. The intent here is to investigate a dice pool mechanic for a game I'm thinking of and model rolling multiple D6s, 5-6 explode, 3+ successes and 1-2 failures. Ultimately I want it to run roll the dice return the dice list number of successes etc and then reset asking the user for number of dice to roll again.

import random
Scount = 0
Xcount = 0
Fcount = 0
rollcount = 0
cheat = 0
NoOfDice = 1
Dicelist = []
while True:
    print ("Input number of dice to roll")
    NoOfDice=input()
    while cheat<int(NoOfDice):
        rand = random.randint(1, 6)
        Dicelist.append(rand)
        if rand <= 4:
            cheat += 1
        if rand >= 3:
            Scount += 1
        if rand >= 2:
            Fcount += 1
        if rand <= 5:
            Xcount += 1
print (Dicelist)
print ("We rolled " + str(NoOfDice) + " you got " + str(Scount) + " number of succeses with " + str(Xcount) + " number of dice exploded with " + str(Fcount) + " dice failed")

Thank you all and appreciate your time!

Upvotes: 1

Views: 110

Answers (2)

Adi219
Adi219

Reputation: 4814

The condition for your first while loop is essentially going to always be True, meaning that it's an infinite loop.

Your second loop may not seem like it's running, but it definitely is (as long as you enter a number greater than 0).

The reason your program has no output is because your print() statements are after the infinite loop, so they'll never run. This is why your program runs as desired when you remove the infinite loop.

To fix this, just move your print() statements inside the first loop but at the end.

Note: If you want to get time to read what is being printed when you run the program, you should change the print()s to input()s as this will mean that the first loop only loops around after you've pressed Enter.

Additional Note: random.randint(1, 6) returns a value from 0 to 5 not from 1 to 6. Looking at your values in your if statements, you probably want to change code to:

rand = random.randint(1,6) + 1

Upvotes: 1

dreamzboy
dreamzboy

Reputation: 841

Your code could further be condensed using with just 2 if statements instead of 4. Below is my proposed solution. I wrapped it in a function but you don't have to. I'll leave it for you think and decide how to escape the first "while" loop after the second while loop finished execution.

def rolldice ():
...     while True:
...         scount = 0
...         xcount = 0
...         fcount = 0
...         cheat = 0
...         NoOfDice = ''
...         DiceList = []
...         NoOfDice = input ('Number of dice to roll: ')
...         while cheat < int (NoOfDice):
...             rand = random.randint(1, 6)
...             DiceList.append (rand)
...             if rand <= 5:
...                 cheat += 1
...                 xcount += 1
...             if rand >= 2:
...                 fcount += 1
...                 scount += 1
...         print ('Dice List: ', DiceList)
...         print ('Number of dice rolled:', NoOfDice)
...         print ('Success Count: %d' % scount)
...         print ('Exploded Count: %d' % xcount)
...         print ('Failed Count: %d' % fcount)
... 
>>> rolldice()

Number of dice to roll: >? 3
Dice List:  [2, 3, 1]
Number of dice rolled: 3
Success Count: 2
Exploded Count: 3
Failed Count: 2

Number of dice to roll: >? 1
Dice List:  [5]
Number of dice rolled: 1
Success Count: 1
Exploded Count: 1
Failed Count: 1

Number of dice to roll:

Upvotes: 0

Related Questions