使用while的全局变量

问题描述 投票:-1回答:1

我正在尝试在代码中增加全局变量,但是当我使用关键字global时,它说n已经被使用了。我正在尝试增加n,以便可以将每个人1到27分配给一个团队。在此先感谢

 my_team =  27 % 4
 team_1 = ""
 team_2 = ""
 team_3 = ""
 team_4 = ""
 team_5 = ""
 team_6 = ""
 team_7 = ""
 print(my_team)
 global n
 n = 1
 for n in range(1, 28):


while n <= 4  :
    global n
    team_1 = team_1 + str(n) + " "
    n = n + 1
    if n == 5:
        break

for n in range (4,8):
    n= n + 1
    team_2 = team_2 + str(n)



while n < 13 and n > 8:

    team_3 =team_3 + str(n)
    n= n + 1


while n < 17 and n > 12:

    team_4 = team_4 + str(n)
    n= n + 1


while n < 21 and n > 16:

    team_5 = team_5 + str(n)
    n= n + 1


while n < 25 and n > 20:

    team_6 = team_6 +str(n)
    n= n + 1

while n < 28 and n > 24:

    team_7 = team_7 + str(n)
    n = n+1


print(team_1)
python global-variables
1个回答
0
投票
my_team = 27 % 4
team_1 = ""
team_2 = ""
team_3 = ""
team_4 = ""
team_5 = ""
team_6 = ""
team_7 = ""
print(my_team)
n = 1

while n <= 4:
    team_1 += str(n) + " "
    n += 1

while 4 < n <= 8:
    team_2 += str(n) + " "
    n += 1

while 8 < n <= 12:
    team_3 += str(n) + " "
    n += 1

while 12 < n <= 16:
    team_4 += str(n) + " "
    n += 1

while 16 < n <= 21:
    team_5 += str(n) + " "
    n += 1

while 21 < n <= 25:
    team_6 += str(n) + " "
    n += 1

while 25 < n <= 28:
    team_7 += str(n) + " "
    n += 1

print(team_1)
print(team_2)
print(team_3)
print(team_4)
print(team_5)
print(team_6)
print(team_7)

我已经根据我的目标解决了其他一些问题,并做了一些其他的更改,您可能可以从中学到一些。

© www.soinside.com 2019 - 2024. All rights reserved.