Whats wrong with my python code It should have visual repres
What\'s wrong with my python code? It should have visual representation of the two dice like the example in the picture . But It doesn\'t show them. Can any one please tell me how I can fix the problem? Here is the code: ( https://repl.it/EWxq/0 )
from random import randrange
def rollDice(sizeOfDice, totalRolls):
# Roll the dice and return result
return [randrange(1, sizeOfDice+1) for _ in range(totalRolls)]
def printDiceSkelton(sizeOfDice, dice_roll, baseZero=False, diceEye=\'o \'):
\"\"\"Return the full face of the roll for a dice-size sided dice.\"\"\"
# variables declaring
r = dice_roll if baseZero else dice_roll - 1
# Building a proper dice
if sizeOfDice > 12:
dice_str =\'---------\ |\' \\
+ diceEye[r<1] + \' \' + diceEye[r<5] + \' \' + diceEye[r<7] + \' \' + diceEye[r<3] + \'|\ |\' \\
+ diceEye[r<9] + \' \' + diceEye[r<13] + \' \' + diceEye[r<15] + \' \' + diceEye[r<11] + \'|\ |\' \\
+ diceEye[r<17] + \' \' + diceEye[r<19 ]
elif sizeOfDice > 6:
dice_str = \' ------- \ |{} {} {} {}|\ |{} {}\'.format(*(diceEye[r<i]
for i in [1, 5, 7, 3, 9, 11]))
else:
dice_str = \'+-----+\ | {0} {1} |\ | {2}\'.format(diceEye[r<1], diceEye[r<3], diceEye[r<5])
# Return mirrored dice string with changing middle to get a full face
return dice_str + diceEye[r&1] + dice_str[::-1]
def printDice(sizeOfDice, dice_rolls, baseZero=False, max_width=72, diceEye=\'o \'):
\"\"\"Pretty print all dice_rolls using sizeOfDice-sided dice(s).\"\"\"
# check parameters
if any(roll > sizeOfDice for roll in dice_rolls):
raise ValueError(\'Roll higer than dice size\')
if len(diceEye) != 2:
raise ValueError(\'Excpected two choice for diceEye parameter\')
# Setting up initial values
dice_width = 7 if sizeOfDice > 6 else 5
dice_lines = 7 if sizeOfDice > 12 else 5
outputResult = [\'\'] * dice_lines
print(\'\ {}-sided dice{}: {}\'.format(sizeOfDice, \', zero-based\' if baseZero else \'\', dice_rolls))
# printing dice
for roll in dice_rolls:
# draw dice skelton
current_dice = printDiceSkelton(sizeOfDice, roll, baseZero, diceEye)
if len(outputResult[0]) + dice_width >= 72:
for idx, line in enumerate(outputResult):
print(line)
outputResult[idx] = \'\'
# appending result
for idx, line in enumerate(current_dice.split(\'\ \')):
outputResult[idx] += line + \' \'
# Print other dices
if len(outputResult[0]) > 0:
for line in outputResult:
print(line)
if __name__ == \'__main__\':
while True:
dice_rolls = rollDice(6,2);
printDice(6, dice_rolls, diceEye=\'* \')
total = sum(dice_rolls)
print(\'Sum of dices : \',total)
if(total == 7 or total == 11):
print(\'wins\')
break;
elif(total == 2 or total == 3 or total == 12):
print(\'you lose\')
break;
Solution
def rollDice(sizeOfDice, totalRolls):
# Roll the dice and return result
return [randrange(1, sizeOfDice+1) for _ in range(totalRolls)]
def printDiceSkelton(sizeOfDice, dice_roll, baseZero=False, diceEye=\'o \'):
\"\"\"Return the full face of the roll for a dice-size sided dice.\"\"\"
# variables declaring
r = dice_roll if baseZero else dice_roll - 1
# Building a proper dice
if sizeOfDice > 12:
dice_str =\'---------\ |\' \\
+ diceEye[r<1] + \' \' + diceEye[r<5] + \' \' + diceEye[r<7] + \' \' + diceEye[r<3] + \'|\ |\' \\
+ diceEye[r<9] + \' \' + diceEye[r<13] + \' \' + diceEye[r<15] + \' \' + diceEye[r<11] + \'|\ |\' \\
+ diceEye[r<17] + \' \' + diceEye[r<19 ]
elif sizeOfDice > 6:
dice_str = \' ------- \ |{} {} {} {}|\ |{} {}\'.format(*(diceEye[r<i]
for i in [1, 5, 7, 3, 9, 11]))
else:
dice_str = \'+-----+\ | {0} {1} |\ | {2}\'.format(diceEye[r<1], diceEye[r<3], diceEye[r<5])
# Return mirrored dice string with changing middle to get a full face
return dice_str + diceEye[r&1] + dice_str[::-1]
def printDice(sizeOfDice, dice_rolls, baseZero=False, max_width=72, diceEye=\'o \'):
\"\"\"Pretty print all dice_rolls using sizeOfDice-sided dice(s).\"\"\"
# check parameters
if any(roll > sizeOfDice for roll in dice_rolls):
raise ValueError(\'Roll higer than dice size\')
if len(diceEye) != 2:
raise ValueError(\'Excpected two choice for diceEye parameter\')
# Setting up initial values
dice_width = 7 if sizeOfDice > 6 else 5
dice_lines = 7 if sizeOfDice > 12 else 5
outputResult = [\'\'] * dice_lines
print(\'\ {}-sided dice{}: {}\'.format(sizeOfDice, \', zero-based\' if baseZero else \'\', dice_rolls))
# printing dice
for roll in dice_rolls:
# draw dice skelton
current_dice = printDiceSkelton(sizeOfDice, roll, baseZero, diceEye)
if len(outputResult[0]) + dice_width >= 72:
for idx, line in enumerate(outputResult):
print(line)
outputResult[idx] = \'\'
# appending result
for idx, line in enumerate(current_dice.split(\'\ \')):
outputResult[idx] += line + \' \'
# Print other dices
if len(outputResult[0]) > 0:
for line in outputResult:
print(line)
if __name__ == \'__main__\':
while True:
dice_rolls = rollDice(6,2);
printDice(6, dice_rolls, diceEye=\'* \')
total = sum(dice_rolls)
print(\'Sum of dices : \',total)
if(total == 7 or total == 11):
print(\'wins\')
break;
elif(total == 2 or total == 3 or total == 12):
print(\'you lose\')
break;


