【问题标题】:how do i print the 2d list as an even table?我如何将二维列表打印为偶数表?
【发布时间】:2022-11-26 21:15:09
【问题描述】:
#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()

我不确定如何均匀地隔开这些值,以便它们在打印时排成一行。

我尝试在打印时在值之间添加空格,但如果其中一个名称或数字的长度不同,则整行将与该列不对齐。

【问题讨论】:

标签: python arrays list multidimensional-array


【解决方案1】:
l1 = [0,1,2]
l2 = ['A','B','C']

for i1,i2 in zip(l1,l2):
    print(f'{i1} | {i2}')

此代码将打印出来

0 | A
1 | B
2 | C

请注意,如果 2 个列表的长度不够,则不会为两个列表打印该索引。例如,如果 l1=[0,1] 它将打印出:

0 | A
1 | B

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-24
    相关资源
    最近更新 更多