这是我昨天提出的问题的后续问题。我是 python 的新手,目前正在使用 Zed Shaw 的 LPTHW 自学。下面的代码已损坏,我可以使用一些帮助来修复它,我正在尝试制作一个用于 RPG 的随机数生成器。
from random import randint
name = raw_input('\n Hello there. Please Type in your name > ')
print """
Hello {} & welcome to the Random Number Generator by Ray Weiss.
""".format(name)
upper = int(raw_input('Enter the type of dice you want to roll > '))
n = int(raw_input("How many D{} you'd like to roll? ".format(upper)))
for _ in xrange(n):
print randint(1, upper)
prompt = """
Would you like to roll another D{}? Type 'yes', 'no', or 'quit'
""".format(upper)
answer = raw_input(prompt)
while answer == "yes":
for _ in xrange(n):
print randint(1, upper)
prompt = """
Would you like to roll another D{}? Type 'yes', 'no', or 'quit'
""".format(upper)
answer = raw_input(prompt)
if answer == "no":
upper = int(raw_input('Enter the type of dice you want to roll > '))
n = int(raw_input("How many D{} you'd like to roll? ".format(upper)))
for _ in xrange(n):
print randint(1, upper)
prompt = """
Would you like to roll another D{}? Type 'yes', 'no', or 'quit'
""".format(upper)
answer = raw_input(prompt)
if answer == "quit":
print """
Thank you {} for using the D{} RNG by Ray Weiss! Goodbye!
""".format(name, upper)
理想情况下,我想要发生的是它首先询问我想要多少面骰子以及想要滚动多少骰子。在那之后,我想让它问我是否想再次滚动相同的骰子和数量,如果我说是,那么它就会这样做。如果我说不,我希望它回过头来问我什么面骰子以及我想再次滚动多少骰子。
编辑:我认为这段代码好一点,但它仍然无法正常工作
from random import randint
def roll(sides, num_of_dices):
return [randint(1, sides) for i in range(num_of_dices)]
prompt = "> "
# Main Program
num_of_dices = input(prompt)
sides = input(prompt)
while True:
results = roll(sides, num_of_dices)
print results
s = input(prompt)
if s == 'quit':
break