我有下面的代码,并试图缩短它。我试过使用while和for循环,但不能使它工作。我也在这里搜索了Stackoverflow,发现了枚举和循环循环,但不断发现错误或者一般不知道我在做什么。有什么方法可以缩短这个吗?我可以使用循环缩短我的代码吗?
我唱python 3.2 pygame兼容版本和idlex。
players = [npc1,npc2,npc3,human] # these are classes
# sets new order of players after being mixed
first_player = players[0]
second_player = players[1]
third_player = players[2]
fourth_player = players[3]
# sets players prey...goes one ahead in the index, wrap around at end
first_players_prey = players[1]
second_players_prey = players[2]
third_players_prey = players[3]
fourth_players_prey = players[0]
# sets players predator, goes back one in the index, wrap around
first_players_predator = players[3]
second_players_predator = players[0]
third_players_predator = players[1]
fourth_players_predator = players[2]
# sets players grand prey/predator while only 4 players, goes 2 ahead/back in index, wrap around
first_players_grand_prey_predator = players[2]
second_players_grand_prey_predator = players[3]
third_players_grand_prey_predator = players[0]
fourth_players_grand_prey_predator = players[1]
当你创建16个单独的变量时,没有可靠的方法可以循环缩短代码。 –