简体   繁体   English

如何根据用户输入创建不同的变量

[英]How to create different variables based off user input

For a monopoly like game I am making I have a section that asks the user the name of the different players.对于我正在制作的类似垄断的游戏,我有一个部分会询问用户不同玩家的姓名。

The number of times, this is asked will be based off of a previous input which asks the user how many players are there.询问的次数将基于先前的输入,该输入询问用户那里有多少玩家。

And from that, there would be different variables set for the number of player.因此,将为玩家数量设置不同的变量。

IE, if the number of players stated in the input was 4, then there would be variables for player 1 with their inputted name and so on to player4. IE,如果输入中声明的玩家数量是 4,那么玩家 1 就会有变量和他们输入的名字,依此类推到玩家 4。

How would I create a number of variables based on the number of players inputted and also ask the names of each player?我将如何根据输入的玩家数量创建多个变量并询问每个玩家的姓名?

The variables would look somewhat like this:变量看起来有点像这样:

player1 = John
player2 = Doe
player3 = Sam

You probably want a single variable.您可能需要一个变量。 This could either be a list or a dict .这可以是一个list或一个dict In either case, you'd probably want a dict inside either of these to represent information about each player.在任何一种情况下,您可能都希望在其中任何一个中都有一个dict来表示有关每个玩家的信息。 So the list you would build up would look like this:所以你要建立的list看起来像这样:

players = [
    {
        "name": "Joe",
        "money": 1234,
        "properties": [
            "Boardwalk",
        ]
        "railroads": [
            "Reading",
        ],
        ...
    },
    {
        "name": "Sam",
        "money": 4444,
        "get_out_of_jail_card": true,
        ...
    }
}

You could build up this list as you take information about each player.您可以在获取有关每个玩家的信息时建立此列表。 Start with an empty list, and then add players:从一个空列表开始,然后添加玩家:

startingMoneyValue = 20000

...

player_count = int(input("Enter number of players: "))

players = []

for i in range(player_count):
    playerName = input("What is the next player's name?")
    players.append({
        "name": playerName,
        "money": startingMoneyValue,
        "properties": [],
        "railroads": [],
        ...
    })

The key here is that once you've created each player's record, you can get at and modify that data easily.这里的关键是,一旦您创建了每个玩家的记录,您就可以轻松获取和修改该数据。 For example, to get the amount of money the second player has, you would do this:例如,要获得第二个玩家拥有的金额,您可以这样做:

money = players[1]['money']

and when player #3 passes go:当球员 #3 传球时:

players[2]['money'] += 200

You can initialize all of the particular values you associate with each player up front, or you could add some of the values later, as things happen.您可以预先初始化与每个玩家相关联的所有特定值,或者您可以稍后添加一些值,以备不时之需。

You'll want to loop through a range based on the number of players.您需要根据玩家数量循环一个范围。 I don't think you can generate variables in a script like you are asking.我认为您不能像您要求的那样在脚本中生成变量。 But a list works just as good.但是列表同样有效。

number_of_players = input("Enter number of players: ")
players = []
for player in range(number_of_players):
    players.append(input(f'Enter player {player+1} name: '))

You can use a dictionary:您可以使用字典:

playerdict, players = {}, int(input('How many players are there?'))

for x in range(players):
    playerdict[f'player{x+1}'] = input(f'What is player {x+1}\'s name?')

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

相关问题 如何根据用户输入创建变量? (Python) - How do you create variables based on user input? (Python) 如何基于用户输入创建正则表达式(Python) - How to create a Regex based on user input (Python) 如何根据用户输入创建不同数量的条目小部件 - How do I create different number of entry widgets based on user input 将用户输入拆分为不同的变量 - Split User input into different Variables 我如何创建一个函数,该函数根据在Python中输入相同输入的次数,使用户输入具有不同的结果? - How do I create a function that allows for user input to have a different outcomes based on how many times the same input was entered in Python? 如何根据用户输入在tkinter中传递不同的命令 - How to pass different commands in tkinter based on user input 如何使用基于函数内用户输入的方法来更改类 - How to alter a class using a method based off user input within a function 您如何根据用户输入在哈希表中添加某些值? - How do you add up certain values in hashtable based off of user input? 如何使用 pandas 并搜索基于用户输入的电子表格来查找行号? - How to find row # using pandas and searching Spreadsheet Based Off User input? 在 python 中使用 Turtle,如何根据用户输入绘制大量形状? - Using Turtle in python, how could one make an amount of shapes be drawn based off of user input?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM