我在python中对齐结果时遇到问题

时间:2019-09-30 06:09:51

标签: python

#Very simple frequency checking project 
import random

def rollingadie(x, y) :
    die=[0,0,0,0,0,0]
    Ratio=[0,0,0,0,0,0]
    random.seed(y)
    for times in range(x) :
        roll = random.randrange(1,7) #roll a die and count the freqeucny and save it on arrary
        if roll == 1 :
            die[0] +=1
        elif roll ==2 :
            die[1] +=1
        elif roll ==3 :
            die[2] +=1
        elif roll ==4 :
            die[3] +=1
        elif roll ==5 :
            die[4] +=1
        elif roll ==6 :
            die[5] +=1

    print(f'Die Face{"Frequency":>13}{"Ratio":>13}')
    for index, value in enumerate(die) :
        print(f'{index+1:>8}{value:>13}{(value/times)*100:>13.1f}') #print out the result of frequency
        Ratio[index]= int((value/times)*100) 
    print()

    print(f'Die Face{"Ratio":>13}{"Bar Chart":>15}')
    for index, value in enumerate(Ratio):
        print(f'{index+1:>8}{Ratio[index]:>13}{"*"*Ratio[index]}') # Display the frequency with *

嗨!这对你们来说可能是非常简单的代码。但是我在对齐打印件时遇到麻烦。 Trouble

如您所见..星星从左对齐。我要从左开始,让我认识 通过查看右侧的星数差异。我该怎么办?

预先感谢

3 个答案:

答案 0 :(得分:1)

您只需要将对齐字符从>翻转到<

f'{index+1:<8}{Ratio[index]:<13}{"*"*Ratio[index]}')

答案 1 :(得分:1)

只需在第二张印刷品的比例后面加上“,” 代码:

import random

def rollingadie(x, y) :
    die=[0,0,0,0,0,0]
    Ratio=[0,0,0,0,0,0]
    random.seed(y)
    for times in range(x) :
        roll = random.randrange(1,7) #roll a die and count the freqeucny and save it on arrary
        if roll == 1 :
            die[0] +=1
        elif roll ==2 :
            die[1] +=1
        elif roll ==3 :
            die[2] +=1
        elif roll ==4 :
            die[3] +=1
        elif roll ==5 :
            die[4] +=1
        elif roll ==6 :
            die[5] +=1

    print(f'Die Face{"Frequency":>13}{"Ratio":>13}')
    for index, value in enumerate(die) :
        print(f'{index+1:>8}{value:>13}{(value/times)*100:>13.1f}') #print out the result of frequency
        Ratio[index]= int((value/times)*100) 
    print()

    print(f'Die Face{"Ratio":>13}{"Bar Chart":>15}')
    for index, value in enumerate(Ratio):
        #CHANGE
        print(f'{index+1:>8}{Ratio[index]:>13}',f'{"*"*Ratio[index]}') # Display the frequen
rollingadie(19, 6)

输出:

Die Face    Frequency        Ratio
       1            4         22.2
       2            2         11.1
       3            4         22.2
       4            4         22.2
       5            2         11.1
       6            3         16.7

Die Face        Ratio      Bar Chart
       1           22 **********************
       2           11 ***********
       3           22 **********************
       4           22 **********************
       5           11 ***********
       6           16 ****************

如果您想在Ratio和bar之间添加更多空间,只需添加诸如
 “”,“”,“”,

答案 2 :(得分:0)

您只需要格式化星号,使它们保持对齐状态:

print(f'{index+1:>8}{Ratio[index]:>13}{"*"*Ratio[index]:<}')

您可能还需要指定可以动态执行的宽度(以下内容也留有整洁的空间。

print(f'{index+1:>8}{Ratio[index]:>13}  {"*"*Ratio[index]:<{max(Ratio)}}')