【问题标题】:How can I create variables in Python using other variables?如何使用其他变量在 Python 中创建变量?
【发布时间】:2023-03-27 21:21:02
【问题描述】:

主要前提是获取两个团队的名字并在他们后面显示一个图标,所以我创建了类似的字典

home_team = {'home_foo': '[](#icon-foo) Foo'}
away_team = {'away_bar': '[](#icon-bar) Bar'}

我的问题是,如何输入团队名称,并创建一个名为home_'team name' 的变量,以便使用字典?

【问题讨论】:

    标签: python variables dictionary python-3.5


    【解决方案1】:

    如果我正确理解你的问题,我想你可能只想做这样的事情:

    teams = {}
    
    team_name = input("Enter your team name: ")
    teams[team_name] = ...
    

    只需将... 替换为该键所需的特定值即可。

    【讨论】:

      猜你喜欢
      • 2020-04-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-29
      • 1970-01-01
      • 2021-02-10
      • 2021-08-31
      相关资源
      最近更新 更多