Derek Pruitt
Derek Pruitt

Reputation: 27

Trying to get my random Dice generator to continue to give random nums

I am writing an example/assignment for my boot camp.

The problem is that i cant get it to continue to give random numbers without killing the program and starting over. I have tried just calling the program again on the else but it didn't work.

I have looked at w3school and geeksforgeeks but cannot see any solutions.

Please could you help?

import random

        
class Dice():
    _Dice20 = random.randrange(1,21) #Assignment of using private and protected.
    __Dice12 = random.randrange(1,13)
    Dice100 = random.randrange(1,101)
    Dice10 = random.randrange(1,11)
    Dice8 = random.randrange(1,9)
    Dice6 = random.randrange(1,7)
    Dice4 = random.randrange(1,5)
    Dice3 = random.randrange(1,4)
    def useDice():
            D = input("Please select D100, D20, D12, D10, D8, D6, D4, D3...\n   >")
            if D == 'D12':
                print('{}'.format(Dice.__Dice12))
                Dice.useDice()
            elif D == 'D20':
                print('{}'.format(Dice._Dice20))
                Dice.useDice()
            elif D == 'D100':
                print('{}'.format(Dice.Dice100))
                Dice.useDice()
            elif D == 'D10':
                print('{}'.format(Dice.Dice10))
                Dice.useDice()
            elif D == 'D8':
                print('{}'.format(Dice.Dice8))
                Dice.useDice()
            elif D == 'D6':
                print('{}'.format(Dice.Dice6))
                Dice.useDice()
            elif D == 'D4':
                print('{}'.format(Dice.Dice4))
                Dice.useDice()
            elif D == 'D3':
                print('{}'.format(Dice.Dice3))
                Dice.useDice()
            else:
                print('You did not select a D100, D20, D12, D10, D8, D6, D4, D3\n')
                YorN = input('Use again? Y/N\n')
                if YorN == 'Y':
                    Dice.useDice()
                else:
                    quit()
                


if __name__ == '__main__':
    Dice.useDice()

Upvotes: 0

Views: 127

Answers (3)

Gebodal
Gebodal

Reputation: 405

A while loop is your friend here, for the repetition.

However, there are some deeper problems. Notably, you've "pre-rolled" all the dice sizes, giving you only one possible outcome for each die size for the Dice class you have (and a new instance won't help, as you've made then class-level variables).

def use_dice():
    while True: # Will keep running until stopped with "break" statement
        d_in = input("Please select D100, D20, D12, D10, D8, D6, D4, D3...\n   >")
        result = None
        if d_in == 'D12':
            result = random.randrange(1,13)
        elif d_in == 'D20':
            result = random.randrange(1,21)
        elif MORE DICE:
            ## Do stuff
        else:
            print('You did not select a D100, D20, D12, D10, D8, D6, D4, D3\n')
            yes_or_no = input('Use again? Y/N\n')
            if yes_or_no != 'Y': # If anything but Y is given, end loop
                break ## The loop will stop if this line is reached
                ## Note no use of quit(), as we may want execution to be returned to us

        if result is not None:
            print('{}'.format(result))

    print('Goodbye') ## This will execute just before function exits

More generally, you maybe want to consider if this should be a class at all. I would say this would work best as a function - the above code will work as one, as it does not rely on any class features. Perhaps you plan on adding extra features that would make it a class, but even then I might split off this functionality into its own function, as it needs no external information, and as a function can be used in more contexts if desired.

P.S. Python convention is to use upper-camel-casing names for classes (MyFoo), and lower case for pretty much everything else with underscores for separation ("use_dice").

Upvotes: 0

egemenakturk
egemenakturk

Reputation: 375

The problem is you define dice100 etc. values just once. You have to get random value each time.

import random

    
class Dice():
    def useDice():
            D = input("Please select D100, D20, D12, D10, D8, D6, D4, D3...\n   >")
            if D == 'D12':
                print('{}'.format(random.randrange(1,13)))
                Dice.useDice()
            elif D == 'D20':
                print('{}'.format(random.randrange(1,21)))
                Dice.useDice()
            elif D == 'D100':
                print('{}'.format(random.randrange(1,101)))
                Dice.useDice()
            elif D == 'D10':
                print('{}'.format(random.randrange(1,11)))
                Dice.useDice()
            elif D == 'D8':
                print('{}'.format(random.randrange(1,9)))
                Dice.useDice()
            elif D == 'D6':
                print('{}'.format(random.randrange(1,7)))
                Dice.useDice()
            elif D == 'D4':
                print('{}'.format(random.randrange(1,5)))
                Dice.useDice()
            elif D == 'D3':
                print('{}'.format(random.randrange(1,4)))
                Dice.useDice()
            else:
                print('You did not select a D100, D20, D12, D10, D8, D6, D4, D3\n')
                YorN = input('Use again? Y/N\n')
                if YorN == 'Y':
                Dice.useDice()
                else:
                    quit()
            


if __name__ == '__main__':
    Dice.useDice()

Upvotes: 0

user13882583
user13882583

Reputation:

Hi Derek and welcome to Stackoverflow,

The random.randrange(1,x) functions are only executed once when the class is initialized. I would recommend you to create a function which generate the random number, for example:

def dice(size):
    return random.randrange(1, size+1)

Have a nice day

Upvotes: 2

Related Questions