我有下面的代碼,並試圖縮短它。我試過使用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個單獨的變量時,沒有可靠的方法可以循環縮短代碼。 –