对于我yesterday的问题,这是一个后续问题。我是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
答案 0 :(得分:0)
所以,你的程序将是这样的:
def roll(sides, num_of_dices):
# returns some list
# main program
sides = input(...)
num_of_dices = input(...)
while True:
results = roll(sides, num_of_dices)
print results
s = input(...)
if s == 'quit':
break
首先尝试单独实施和测试roll
,当它准备就绪时,将其与主程序集成。
关于实现或roll
,滚动N个骰子与滚动一个骰子N次相同。在python中,要执行N次操作并收集结果,请使用列表解析:
results = [action for i in range(N)]
所以,你的行动就是掷出一个sides
侧骰子,我们需要重复num_of_dices
次。把它们放在一起,
def roll(sides, num_of_dices):
return [roll_one_dice(sides) for i in range(num_of_dices)]