在python中创建列文本框

在python中创建列文本框,python,Python,我正在做一个学校项目,用python制作Yahtzee(我对这种语言很陌生),我想知道这是否可能,如果可能,如何在命令行中显示一列文本,以显示玩家的分数,每当他们决定在某个类别中得分时,这些分数都会更新。以下是我想要打印的内容: print:(''' ╔═══════════╗╔═══════════╗ ║ Ones ║║ ║ ╠═══════════╣╠═══════════╣ ║ Twos ║║ ║ ╠═══════════╣╠══

我正在做一个学校项目,用python制作Yahtzee(我对这种语言很陌生),我想知道这是否可能,如果可能,如何在命令行中显示一列文本,以显示玩家的分数,每当他们决定在某个类别中得分时,这些分数都会更新。以下是我想要打印的内容:

print:('''
╔═══════════╗╔═══════════╗
║ Ones      ║║           ║
╠═══════════╣╠═══════════╣
║ Twos      ║║           ║
╠═══════════╣╠═══════════╣
║ Threes    ║║           ║
╠═══════════╣╠═══════════╣
║ Fours     ║║           ║
╠═══════════╣╠═══════════╣
║ Fives     ║║           ║
╠═══════════╣╠═══════════╣
║ Sixes     ║║           ║
╠═══════════╣╠═══════════╣
║ Total     ║║           ║
╠═══════════╬╬═══════════╬
╠═══════════╬╬═══════════╬
║ Three of  ║║           ║
║ a kind    ║║           ║
╠═══════════╣╠═══════════╣
║ Four of   ║║           ║
║ a kind    ║║           ║
╠═══════════╣╠═══════════╣
║ Full House║║           ║
╠═══════════╣╠═══════════╣
║ Small     ║║           ║
║ Straight  ║║           ║
╠═══════════╣╠═══════════╣
║ Large     ║║           ║
║ Straight  ║║           ║
╠═══════════╣╠═══════════╣
║ Chance    ║║           ║
╠═══════════╣╠═══════════╣
║ Yahtzee   ║║           ║
╚═══════════╝╚═══════════╝
''')

我想第二列被复制和更新变量取决于球员的数量和他们在每个类别中的得分。任何想法都会很有帮助。

如果您使用的是python 3.6,那么将是理想的选择。


使用大括号添加t变量,并在使用文本变量时进行转换。

您可以预定义框的固定长度,然后打印值并计算有差异的空格,以保留结构,如下所示:

box_design_length = 10 # box design length to store character
ones = 2
twos = 55
threes = 4596


print('''
╔═══════════╗╔═══════════╗
║ Ones      ║║ {0}{1}║
╠═══════════╣╠═══════════╣
║ Twos      ║║ {2}{3}║
╠═══════════╣╠═══════════╣
║ Threes    ║║ {4}{5}║
╚═══════════╝╚═══════════╝
'''.format(ones, ' '*(box_design_length-len(str(ones))),
    twos, ' '*(box_design_length-len(str(twos))), 
    threes, ' '*(box_design_length-len(str(threes))), 
    )
)
format()
do字符串格式化
{0}、{1}、{2}…
是在支持2.6以上所有python版本的
format()
中传递的参数数

输出:

╔═══════════╗╔═══════════╗
║ Ones      ║║ 2         ║
╠═══════════╣╠═══════════╣
║ Twos      ║║ 55        ║
╠═══════════╣╠═══════════╣
║ Threes    ║║ 4596      ║
╚═══════════╝╚═══════════╝
如果您知道,您可以用如下所示的更好的方式进行:

from collections import OrderedDict
values = OrderedDict([('Ones', 2), ('twos', 55), ('threes', 4596)])  # store key, value in dictionary
# order dictionary is used to preserve order

def box_printer(value_set, box_design_length):
    """
     print values in box
    :param value_set: dictionary of key and value to print in box
    :param box_design_length: length of box design
    """
    count = 0  # initialize count: help to identify first loop or last loop to change box design accordingly

    for key, value in value_set.items():
        if count == 0: # first loop
            print('╔{0}╗╔{0}╗'.format('═'*box_design_length))
        count += 1
        print('║ {1:^{0}}║║ {2:^{0}}║'.format(box_design_length-1, key, value))
        if count >= len(value_set):
            print('╚{0}╝╚{0}╝'.format('═'*box_design_length))
        else:
            print('╠{0}╣╠{0}╣'.format('═'*box_design_length))

box_printer(values, 11)

您将使用此代码获得所需的输出。

他的列设计将取决于
t中的数字长度
如果t将更高或更低,则预期设计将不会看起来像same@Gahan同意必须在数值上定义一个限值,以符合设计。如果已知最大值(即100),则变量t的零填充将解决较小值的问题。i、 e{t:03d}会将t填充到003只有yahtzee类别有数字100所有其他的只有两个数字你能根据玩游戏的人数将盒子横向扩展吗?这样地:╔═══════════╗╔═══════════╗╔═══════════╗ ║ 一个║║ ║║ ║ ╠═══════════╣╠═══════════╣╠═══════════╣ ║ 两个║║ ║║ ║ ╠═══════════╣╠═══════════╣╠═══════════╣ ║ 三║║ ║║ ║ ╚═══════════╝╚═══════════╝╚═══════════╝@BrianMcglinchey你能详细说明一下吗?
from collections import OrderedDict
values = OrderedDict([('Ones', 2), ('twos', 55), ('threes', 4596)])  # store key, value in dictionary
# order dictionary is used to preserve order

def box_printer(value_set, box_design_length):
    """
     print values in box
    :param value_set: dictionary of key and value to print in box
    :param box_design_length: length of box design
    """
    count = 0  # initialize count: help to identify first loop or last loop to change box design accordingly

    for key, value in value_set.items():
        if count == 0: # first loop
            print('╔{0}╗╔{0}╗'.format('═'*box_design_length))
        count += 1
        print('║ {1:^{0}}║║ {2:^{0}}║'.format(box_design_length-1, key, value))
        if count >= len(value_set):
            print('╚{0}╝╚{0}╝'.format('═'*box_design_length))
        else:
            print('╠{0}╣╠{0}╣'.format('═'*box_design_length))

box_printer(values, 11)