尽管只需要执行部分代码,但代码仍执行所有行

时间:2020-10-19 17:23:51

标签: python

它是这样的,当我输入一个查询时,它会被解析,并提取两个值,这是日期和指标,并最初获得对该日期的适当响应

response = {'l_result': [{'deaths': 5, 'tested': 5,}]}

if value('DATE'):
     print("===================")
     print("this is the response ")
     print(response)
     print("===================")

if value('indicator'):
mapped_indicator = values('indicator')[-1]
   if mapped_indicator == 'Tested':
      tested_list = [
       {'total_tested': response.get('tested'),
        'new_tests': response.get('new_tests')}]
          print(tested_list)


我的问题是所有代码都在运行。 我希望如果仅找到日期,则将显示所有响应信息,如果找到日期和指标,则仅显示指标的特定信息。

我的输出是

===================
this is the response
{'l_result': [{'deaths': 5, 'tested': 5,}]}
===================

tested: 5

您看到整个代码的结果在那里,而我想要的只是

tested: 5

你能告诉我怎么做吗

2 个答案:

答案 0 :(得分:1)

只是给您提供了一个简单的示例,希望这对您有帮助,不是字典结构

date = '10/19/2020'
indicator = 'Y'

if  indicator =='Y' and date == '10/19/2020':
    print('Indicator is ',indicator)
elif date == '10/19/2020':
    print('Only date has a value')


print('2nd case when indicator is empty')

date = '10/19/2020'
indicator = None

if  indicator =='Y' and date == '10/19/2020':
    print('Indicator is ',indicator)
elif date == '10/19/2020':
    print('Only date has a value') 

答案 1 :(得分:0)

从您提供的信息来看,您似乎必须使用逻辑运算“和”,然后将其单独使用

如果value('DATE'):和value('indicator')

elif .....