尝试让我的随机Dice生成器继续提供随机数

时间:2020-07-07 07:47:34

标签: python-3.x

我正在为我的新兵训练营编写示例/作业。

问题是我无法让它继续提供随机数而不杀死程序并重新开始。我尝试过在其他地方再次调用该程序,但没有成功。

我看过w3school和geeksforgeeks,但看不到任何解决方案。

请您帮忙?

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()

3 个答案:

答案 0 :(得分:2)

您好,德里克,欢迎来到Stackoverflow,

random.randrange(1,x)函数仅在初始化类时执行一次。我建议您创建一个生成随机数的函数,例如:

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

祝你有美好的一天

答案 1 :(得分:0)

问题是您仅定义一次dice100等值。您每次都必须获得随机值。

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()

答案 2 :(得分:0)

while循环是您的朋友,以供重复。

但是,还有一些更深层次的问题。值得注意的是,您已经“预滚动”了所有骰子的大小,对于您拥有的Dice类,每个骰子大小仅给您一个可能的结果(并且新实例将无济于事,因为您已经达到了类级别变量)。

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

更一般而言,您可能要考虑这是否应该是一门课。我想说这将是最好的功能-上面的代码将作为一个函数工作,因为它不依赖于任何类功能。也许您打算添加额外的功能以使其成为一个类,但是即使那样,我也可以将此功能拆分为自己的功能,因为它不需要外部信息,并且可以根据需要在更多上下文中使用该功能。

P.S。 Python惯例是对类(MyFoo)使用大写字母的外壳名称,对于几乎所有其他名称都使用小写字母,并带有下划线分隔(“ use_dice”)。

相关问题