#Create a program that allows 2 players to throw a 6 sided dice and record the roll value 10 times.
#The winner is the player with the highest total
#This task must store the results in a 2D array
#Extra:
#Work out the average roll across the 10 throws per player and display the result
#Frequency analysis broken down per player and per game
from random import randint
results = [[],[]]
for i in range (2):
player = []
total = 0
average = 0
#player enters their name
name = input(f"\nEnter your name player {i 1}: ")
player.append(name)
print(f"Player {i 1} it is your turn")
for x in range(10):
print("\nTo roll the die press any key")
input()
roll = randint(1,6)
player.append(roll)
print("You rolled a", roll)
total = roll
average = total/10
player.append(total)
player.append(average)
results.append(player)
print("""\nNAME R1 R2 R3 R4 R5 R6 R7 R8 R9 R10 TOTAL AVG""")
for i in results:
for c in i:
print(c,end = " ")
print()
im not sure how to evenly space out the values so they are in line when they are printed.
i tried adding spaces inbetween the values when printing but if one of the names or numbers are a different length then the whole row becomes unaligned with the column.
CodePudding user response:
You can use the following sintax:
print('%5s' % str(c))
Basically:
- the
%
character informs python it will have to substitute something to a token - the
s
character informs python the token will be a string - the
5
(or whatever number you wish) informs python to pad the string with spaces up to 5 characters.
I found that on How to print a string at a fixed width?.
CodePudding user response:
to calculate difference between 2 names
diff = abs(len(results[2][0]) - len(results[3][0])) #abs = absolute value
compare 2 lengths of names:
if len(results[2][0] ) > len(results[3][0]):
results[3][0] = results[3][0] " "*diff # add spaces much as difference between 2 names
else:
results[2][0] = results[2][0] " "*diff