我正在为我的新兵训练营写一个例子/作业。
问题是,我不能让它继续给出随机数字,而不杀死程序,重新开始。我试过再打电话给其他程序,但没成功。
我看过w3school和极客健忘者,但找不到任何解决方案。
你能帮忙吗?
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()
发布于 2020-07-07 08:16:42
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
更广泛地说,您可能想考虑这是否应该是一个类。我想说,作为一个函数,这将是最好的工作-上面的代码将作为一个整体,因为它不依赖于任何类功能。也许您计划添加使其成为类的额外功能,但即使这样,我也可能将此功能拆分到它自己的函数中,因为它不需要外部信息,而且如果需要,作为一个函数可以在更多的上下文中使用。
Python惯例是为类使用上骆驼大小写名称(MyFoo),对几乎所有其他东西都使用小写,下划线表示分离("use_dice")。
发布于 2020-07-07 07:54:31
你好德里克欢迎来到斯塔克沃夫
random.randrange(1,x)函数在初始化类时只执行一次。我建议您创建一个生成随机数的函数,例如:
def dice(size):
return random.randrange(1, size+1)
祝您今天愉快
发布于 2020-07-07 07:57:32
问题是您只定义了一次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()
https://stackoverflow.com/questions/62770434
复制相似问题