简体   繁体   English

根据另一个变量创建变量

[英]Creating variables depending of another variable

For a school project we have to write a mini game. 对于学校项目,我们必须编写一个迷你游戏。 I got stuck when I tried to create new variables in a loop. 当我尝试在循环中创建新变量时,我陷入了困境。 I wrote a code where the user has to give the number of the players, and the Idea was to create new variables in a loop for each player. 我编写了一个代码,其中用户必须提供玩家人数,其主意是在循环中为每个玩家创建新变量。 My question would be 1st : Is it even possible? 我的问题是第一:甚至可能吗? 2nd : I want to store more information in a variable ( weapon, vest etc...) , so should I prefer using dictionary? 第二:我想将更多信息存储在变量中(武器,背心等...),所以我更喜欢使用字典吗?

1st: Make a list, and use subsequent elements of that list. 第一个:创建一个列表,并使用该列表的后续元素。 You can't create a variable with the same name twice. 您不能两次创建相同名称的变量。

2nd: I would go with a dictionary. 第二:我会去字典。

EG: 例如:

playerdata= []
for player in numplayers:
   playerdata.append({'playerID':player, 'weapon':'bow'})

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

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