【发布时间】:2019-03-04 18:09:43
【问题描述】:
我应该编写一个程序来掷骰子五次,但我不能有任何冗余,也不能使用任何类型的循环。 我将在此处粘贴练习中的文本。 “构建一个 python 应用程序,以便提示用户输入骰子的面数(龙与地下城风格的骰子!) 在此之后,它将掷骰子五次,并将所有五个结果输出给用户 掷骰子五次将需要五个重复的代码块——通过使用用户定义的函数掷骰子来消除这种冗余
谁能帮帮我?
import random
def main():
diceType = int(input("Enter how many sides the dices will have: "))
diceRoll1 = random.randint(1,diceType)
diceRoll2 = random.randint(1,diceType)
diceRoll3 = random.randint(1,diceType)
diceRoll4 = random.randint(1,diceType)
diceRoll5 = random.randint(1,diceType)
print("The dices read", diceRoll1, diceRoll2, diceRoll3, diceRoll4, diceRoll5)
main()
【问题讨论】:
-
为什么需要在没有循环的情况下这样做?
-
这是来自课堂的挑战。
-
你应该使用
itertools模块吗? -
根据您如何定义“任何类型的循环”和冗余,这项任务介于微不足道和不可能之间。
-
大多数类似这样的挑战指定你可以使用什么;例如,您应该进行递归吗?