-2
我有一段用python編寫的代碼,可以用任意數量的邊來擲骰子。有時輸出爲零,我寫了它,但這樣做只是重新輸入輸入,而不是自動修復輸入。我希望如此,如果輸出爲零,它會重新推出,而不會再詢問。如何在輸出不起作用時停止冗餘輸入?
from random import randrange
def Rolling(b):
a = 1
in1 = raw_input('Roll out of %d' % (b))
if in1 == 'roll' or in1 == 'Roll' or in1 == 'r':
irand = randrange(b)
if irand == 0:
Rolling(b)
else:
print "Your roll is %d out of %d" % (irand, b)
Rolling(b)
elif in1 == 'Change' or in1 == 'change':
in2 = int(raw_input('How many sides on the new die?'))
b = in2
Rolling(b)
elif in1 == 'reroll' or in1 == 'Reroll':
irand = randrange(b)
if irand == 1 or irand == 0:
Rolling(b)
else:
print "Your roll is %d out of %d" % (irand, b)
Rolling(b)
else: print "Please Type <roll in order to roll the dice."
Rolling(b) # using recursion to call again incase of erroneous input
Rolling(10)