python - Trying to print a display row-by-row, by adding rows together in a single, multi-line string -
python - Trying to print a display row-by-row, by adding rows together in a single, multi-line string -
i trying print out series of die display below:
___ ___ ___ ___ ___ |* | |* | |* *| |* | | | | | | * | |* *| | * | | * | | *| | *| |* *| | *| | | ^^^ ^^^ ^^^ ^^^ ^^^
these die should represent numbers produced when 'roll dice'. have created die class , hand class, , 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): homecoming str(self.value)'''
import die class hand: def __init__(self): self.l=[] in range(5): self.l.append(die.die()) #makes number of dice def __str__(self): """converts hand string printing""" homecoming "{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): in range(5): if i+1 not in keep: self.l[i].roll()
the code given above produce list of numbers when main file enters
h = hand.hand() h.roll() #this rolls hand 1 time again print(h)
any ideas how can results print in provided display style? has been suggested best way print 3 rows build 5 dice. recent advice "you have build entire display of dice row-by-row, add together rows in single, multi-line string." i'm not sure how execute that.
here's 1 way it:
die_parts = [ ['___'] * 6, [' ', '* ', '* ', '* *', '* *', '* *'], [' * ', ' ', ' * ', ' ', ' * ', '* *'], [' ', ' *', ' *', '* *', '* *', '* *'], ['^^^'] * 6, ] die_spacing = ' ' * 3 def print_hand(dies): # build line parts lines = [[],[],[],[],[]] i,line in enumerate(lines): d in dies: if in [1,2,3]: border = '|' else: border = ' ' part = '' part += border # leading border of die part += die_parts[i][d-1] # "pips" part += border # trailing border of die line.append(part) # bring together lines lines = [die_spacing.join(l) l in lines] # print lines l in lines: print(l) print_hand([1,2,3,4,5,6])
output:
___ ___ ___ ___ ___ ___ | | |* | |* | |* *| |* *| |* *| | * | | | | * | | | | * | |* *| | | | *| | *| |* *| |* *| |* *| ^^^ ^^^ ^^^ ^^^ ^^^ ^^^if it's not clear, you're building each line of 5 line output, line line, die die.
so goes
line 1 - die 1, line 1 - die 2, line 1 - die 3, ... line 2 - die 1, line 2 - die 2, line 2 - die 3, ... ... line 5 - die 1, line 5 - die 2, line 5 - die 3, ...
then joins parts of lines list using die_spacing
delimiter.
i'll leave integrating rest of code you.
python arrays string python-3.x rows
Comments
Post a Comment