使用带有 Pydev 解释器的 Eclipse 的宾果游戏



请帮助我生成宾果游戏。我正在使用带有"pydev"解释器的日食。到目前为止,我已经工作了这么多。但是我无法以某种方式生成矩阵,即 5-15 之间的随机数应该在第 1 列"中而不是在"ROW"中,同样,第 2 列中 16-30 之间的数字,第 3 列中 31-45,第 4 列中 46-60,最后是第 5 列中的 61-75。所以,基本上它变成了 5*5 矩阵。我想要,我的代码生成随机数,但不是以矩阵形式生成。

import random
c1 = random.sample(range(1,15),5)
c2 = random.sample(range(16,30),5)
c3 = random.sample(range(31,45),5) 
c4 = random.sample(range(46,60),5)
c5 = random.sample(range(61,75),5)
matrix = [c1, c2, c3, c4, c5]  # why the ',' at the end of this line???
print(matrix)

for i in range(len(matrix)):
    for j in range(len(matrix[i])):
        print("%s  "%(matrix[i][j], ) )
    break
print()
for j in range(len(matrix[i])):
    for i in range(len(matrix)):
        print("%s  "%(matrix[i][j],) )    
print()

首先,要有一个从 1 到 15 的范围,你需要使用 range(1, 16) ,第二个参数,称为 stop,永远不会包含在列表中。请参阅文档中的范围函数。

要回答您的问题,您可以使用 zip 函数将第一个列表放在第一列而不是第一行。请参阅文档中的 zip 函数。

matrix = zip(c1, c2, c3, c4, c5)

最后,你应该更多地了解 Python 语法。它真的可以帮助你。它的设计很简单。这是一种非常漂亮的语言,易于阅读和使用。你几乎可以像阅读英语一样阅读每一行。这是重写的循环:

for row in matrix:
    for item in row:
        print(item, end=" ")
        # Note the "end" argument, which replaces the newline by a space
    print()  # Newline at the end of the loop

在此处了解有关 for 循环的更多信息。

最新更新