我回来了,先不多说,上个代码,明天讲解这个代码

willsonlincake之家 willsonlincake之家 1050 人阅读 | 2 人回复 | 2022-10-10

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 willsonlincake 于 2022-10-11 22:44 编辑
  1. import numpy as np
  2. import matplotlib.pyplot as plt
  3. import textwrap as twp

  4. count = 1 # input from user on number of unique bingo cards to generate
  5. size = 5 # Input from user on side length of square bingo matrix
  6. list = [] # list of all possible bingo field fillers
  7. path = './ESC-Bingo-Fields.txt'

  8. print('------ Bingo Generator ------')
  9. try:
  10.     print('Enter number of players: ')
  11.     try:
  12.         count = int(input())
  13.     except ValueError:
  14.         print("Invalid input. Input has to be an integer.")
  15.         exit()

  16.     print('Enter size of bingo card (i.e. for 5x5 enter 5): ')
  17.     try:
  18.         size =int(input())
  19.     except ValueError:
  20.         print("Invalid input. Input has to be an integer.")
  21.         exit()

  22.     print('Enter path to txt file with Bingo entrys:')
  23.     path = input()

  24.     with open(path, encoding='utf8') as f:
  25.         list = f.readlines()
  26.         f.close()

  27.     list = np.array(list)
  28.     try:
  29.         if(list.size < (size*size)):
  30.             raise ValueError
  31.     except ValueError:
  32.         print("To few entrys in file.")
  33.         exit()

  34.     for user in range(count):
  35.         rng = np.random.default_rng()
  36.         list_identifiers = rng.choice(a=list, size=(size**2), replace=False)

  37.         # here: export the stuff to plt
  38.         plt.figure(figsize=(15,15), facecolor='xkcd:light blue grey')
  39.         
  40.         for i in range(1, size**2 + 1):
  41.             subplt = plt.subplot(size, size, i)
  42.             plt.xticks([])
  43.             plt.yticks([])
  44.             plt.text(0.5, 0.5, twp.fill(list_identifiers[i-1], 25), horizontalalignment='center', verticalalignment='center')

  45.         file_name = 'Bingo_nr' + str(user) + '.jpg'
  46.         
  47.         plt.savefig(file_name)

  48. except KeyboardInterrupt:
  49.     print("\nExiting...")
  50.     exit()
复制代码

回答|共 2 个

simonzhd 发表于 2022-10-11 18:01:56| 字数 5 | 显示全部楼层

欢迎回归~
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

热门推荐