【问题标题】:In operator not working when the variable is inputted and works when the value is assignedIn 运算符在输入变量时不起作用,在分配值时起作用
【发布时间】:2020-06-18 20:19:46
【问题描述】:

所以我有一个 yaml 文件。我试图从中提取价值。目前我没有循环它并获得我想要的值,当这个问题解决时会这样做。但是当我输入要使用的变量时,它找不到密钥。否则它会起作用。

import yaml
f = open(path)
blueprints = yaml.load(f, Loader=yaml.FullLoader)
id = 682

if (id in blueprints) == True:
    act = input("\ncopying\ninvention\nproducts\nskills\nmanufacturing\nActivity: ")
    if (act in blueprints[id]["activities"] == True):
        print(blueprints[id]["activities"][act])
    else:
        print("could not find the activity")
else:
    print("could not find id")

所以输出是“找不到活动”,这意味着第一个 in 运算符工作但第二个没有。我尝试通过输入第一个变量“id”而不是“act”来做完全相反的事情,但它也不起作用。是的,我确实将“id”变量转换为整数。 yaml 文件太大,这里是其中的一部分:

681:
    activities:
        copying:
            time: 480
        manufacturing:
            materials:
            -   quantity: 86
                typeID: 38
            products:
            -   quantity: 1
                typeID: 165
            time: 600
        research_material:
            time: 210
        research_time:
            time: 210
    blueprintTypeID: 681
    maxProductionLimit: 300
682:
    activities:
        copying:
            time: 480
        manufacturing:
            materials:
            -   quantity: 133
                typeID: 38
            products:
            -   quantity: 1
                typeID: 166
            time: 600
        research_material:
            time: 210
        research_time:
            time: 210
    blueprintTypeID: 682
    maxProductionLimit: 300

我对 python 还是很陌生,因为我正在学习自己,所以我没有人可以问。如果解决方案很简单,我很抱歉。

【问题讨论】:

  • 如果您的输入实际上是正确的,只需执行act in blueprints[id]["activities"],删除== True。您的input 必须与activities 中的任何键完全匹配。 in 运算符已经返回一个布尔值。
  • 总之,但像这样的东西,if (id in blueprints) == True 是多余的。 if (whatever) == True: 应始终为 if whatever:

标签: python python-3.x operators


【解决方案1】:

以下是所需的更改,请参阅 cmets。 Sample here:

import yaml
# f = open(path)
with open('data.yaml', 'r') as f:   # change to use with context manager which will handle file closing
                                    # Just change 'data.yaml', remember to indent the block.
  blueprints = yaml.load(f, Loader=yaml.FullLoader)
  id_no = 682                       # change to use id_no
                                    # id is a built in function you don't want to overwrite.

  if id_no in blueprints:           # Remove the superfluous == True
      act = input("\ncopying\ninvention\nproducts\nskills\nmanufacturing\nActivity: ")
      if act in blueprints[id_no]["activities"]:  # Remove the erroneous == True, which causes your result to fail
          print(blueprints[id_no]["activities"][act])
      else:
          print("could not find the activity")
  else:
      print("could not find id")

试运行:

copying
invention
products
skills
manufacturing
Activity: copying
{'time': 480}

注意:请记住,您的 input (act) 必须与您在 activities 中的键完全匹配,并且区分大小写。否则,您需要转换案例以进行比较。

【讨论】:

  • 谢谢。我不知道 id 是一个内置函数,也不知道 in 应该以这种方式使用。这行得通,谢谢
猜你喜欢
  • 2022-06-21
  • 2022-08-20
  • 2020-12-12
  • 1970-01-01
  • 1970-01-01
  • 2021-08-28
  • 1970-01-01
  • 1970-01-01
  • 2016-06-25
相关资源
最近更新 更多