繁体   English   中英

使用 Eclipse 和 pydev 解释器的宾果游戏

[英]bingo game using eclipse with pydev interpreter

请帮助我生成宾果游戏。 我正在使用带有“pydev”解释器的 eclipse。 到目前为止,我已经工作了这么多。 但我无法以某种方式生成矩阵,1-15 之间的 5 个随机数应该在第一个“列”中而不是在“行”中,类似的数字在第二列中的 16-30 之间,第三列中的 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 ,永远不会包含在列表中。 请参阅文档中的range 函数

要回答您的问题,您可以使用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 循环的更多信息。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM