【发布时间】:2015-04-04 22:53:16
【问题描述】:
我正在尝试打印一系列骰子,显示如下:
___ ___ ___ ___ ___
|* | |* | |* *| |* | | |
| | | * | |* *| | * | | * |
| *| | *| |* *| | *| | |
^^^ ^^^ ^^^ ^^^ ^^^
这些骰子应该代表我“掷骰子”时产生的数字。我创建了一个 Die 类和一个 Hand 类,它们是在下面生成的。
import random
class Die():
def __init__(self):
self.value = random.randint(1,6)
def roll(self):
self.value = random.randint(1,6)
'''def __str__(self):
return str(self.value)'''
import Die
class Hand:
def __init__(self):
self.L=[]
for i in range(5):
self.L.append(Die.Die()) #makes the number of dice
def __str__(self):
"""converts hand to a string for printing"""
return "{0},{1},{2},{3},{4}".format(self.L[0], self.L[1], self.L[2], self.L[3], self.L[4])
def roll(self, keep):
for i in range(5):
if i+1 not in keep:
self.L[i].roll()
上面给出的代码会在主文件进入时产生一个数字列表
h = hand.Hand()
h.roll() #this rolls the hand again
print(h)
有什么想法可以让我的结果以提供的显示样式打印吗?有人建议最好的方法是打印三行来构建所有 5 个骰子。 最近的建议是“你必须 逐行构建骰子的整个显示,然后将行加在一起 在单个多行字符串中。”我只是不确定如何执行。
【问题讨论】:
标签: python arrays string python-3.x rows