我必须创建一个由两个随机掷骰子组成的函数,并返回组合值。然后我必须使用循环来滚动它以给出五个不同的组合值。
我一直困惑的一点是根据组合滚动来定义循环以获得结果。
我只是不断地获取序列 1 22 第333章 4444 55555
如果您有任何建议或帮助,我们将不胜感激。谢谢。
这是我迄今为止编写的代码
import random
def point():
rollone = random.randint(1, 6)
rolltwo = random.randint(1, 6)
combined_value = dicesum(rollone, rolltwo)
def dicesum(x, y):
result = x + y
return result
for result in range(1,6):
for dicerolls in range(result):
print(result, end='')
print()
point()
最佳答案
您的 point
方法在对结果求和后不会返回任何内容。请参阅下面的代码。
import random
def point():
rollone = random.randint(1,7) # sample (1,7) to get values from 1 to 6
rolltwo = random.randint(1,7)
combined_value = rollone + rolltwo # don't need a helper just to add these
return combined_value # return the combined value
def main():
for roll in range(5):
print "Roll", roll, "Sum", point()
测试
>>> main()
Roll 0 Sum 6
Roll 1 Sum 2
Roll 2 Sum 5
Roll 3 Sum 7
Roll 4 Sum 12
关于python - Python 循环中丢失了掷骰子组合值的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26043500/