繁体   English   中英

我如何让这个 for 循环每 8 次开始一个新行

[英]how do I make this for loop start a new line every 8 times

我如何让这个 for 循环每 8 次开始一个新行:

当前: https : //i.stack.imgur.com/kyJyb.png需要: https : //i.stack.imgur.com/SLN8B.png

def generate_votes(candidates):
    votes = 0
    for i in candidates:
        votes += 1   
    votes *= 10
    print("\nGenerating ",votes," votes between 0 and ", (votes // 10)-1)
    
    for i in range(votes//10):
        for i in range(0,8):
            n = random.randint(0,(votes//10)-1)
            print(n,end='\t')

这是一个简单的修复,

您的代码:

def generate_votes(candidates):
    votes = 0
    for i in candidates:
        votes += 1   
    votes *= 10
    print("\nGenerating ",votes," votes between 0 and ", (votes // 10)-1)
    
    for i in range(votes//10):
        for i in range(0,8):
            n = random.randint(0,(votes//10)-1)
            print(n,end='\t')

变成:

def generate_votes(candidates):
    votes = 0
    for i in candidates:
        votes += 1   
    votes *= 10
    print("\nGenerating ",votes," votes between 0 and ", (votes // 10)-1)
    
    for i in range(votes//10):
        for i in range(0,8):
         print('\n')
            n = random.randint(0,(votes//10)-1)
            print(n,end='\t')

我添加了print('\\n') ,这是一个 python 命令,它将直接在你的程序中开始一个新行。 我将它嵌套在for循环中,因此它只会在第 8 次迭代之后发生。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM