我需要输出以下内容
server1
server2
server3
server4
server5
**********************
server6
server7
server8
server9
server10
**********************
server11
server12
server13
server14
server15
**********************
etc
我有以下代码
import random
s = 0
n = 0
nb = 0
nb0 = 20
nb1 = 5
var1 = "server"
var2 = "**********************"
for i in range(nb0):
while nb < nb0:
s = s + 1
nb = nb + n
for i in xrange(nb1):
while n < nb1:
n = n + 1
print str(var1) + str(s)
print var2
但输出不正确,我无法找到如何在每个循环中继续增加服务器编号,直到达到nb。
server1
server1
server1
server1
server1
**********************
server2
server2
server2
server2
server2
**********************
server3
server3
server3
server3
server3
**********************
感谢您的帮助
答案 0 :(得分:3)
我建议使用modulo运算符,这是一个小型演示:
numservers = 15 # put anything you want here
for i in range(1,numservers+1):
print('server{}'.format(i))
if i%5 == 0:
print('*'*22)