samedi 4 avril 2015

Trying to print a display row-by-row, by adding rows together in a single, multi-line string

I am trying to print out a series of die that will display as below:



___ ___ ___ ___ ___
|* | |* | |* *| |* | | |
| | | * | |* *| | * | | * |
| *| | *| |* *| | *| | |
^^^ ^^^ ^^^ ^^^ ^^^


These die should represent the numbers that are produced when I 'roll the dice'. I have created a Die class and a Hand class, and they are produced below.



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()


The code given above will produce a list of numbers when the main file enters



h = hand.Hand()
h.roll() #this rolls the hand again
print(h)


Any ideas how I can get my results to print in the provided display style? It has been suggested that the best way to do it is to print three rows to construct all 5 dice. The most recent advice was "You have to build the entire display of dice row-by-row, then add the rows together in a single, multi-line string." I'm just not sure how to execute that.


Aucun commentaire:

Enregistrer un commentaire