简体   繁体   English

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

[英]Code executing all lines despite only required to excute a part of code

So it goes like this, When I enter a query it is parsed and two values and pulled out this is the date and indicator and gets the appropriate response to for that date initially所以它是这样的,当我输入一个查询时,它被解析和两个值并拉出这是日期和指标,并最初获得对该日期的适当响应

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)


My problem is that all the code runs.我的问题是所有代码都在运行。 I want that if only a date is found then all information in response will be shown and if a date and indicator are found then only specific information for the indicator is shown.我希望如果只找到一个日期,则将显示所有响应信息,如果找到日期和指标,则只显示该指标的特定信息。

my output is我的输出是

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

tested: 5

as you see the result for the entire code is there and what I wanted was just当你看到整个代码的结果就在那里,我想要的只是

tested: 5

Can you tell me how to do this你能告诉我怎么做吗

just gave you a simple example, hope this helps in your case, this is not with a dictionary structure只是给了你一个简单的例子,希望这对你有帮助,这不是字典结构

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') 

from what information you have provided, it looks like you have to use the logical operation "and" and then have a seperate for从您提供的信息来看,您似乎必须使用逻辑运算“and”,然后单独使用

if value('DATE'): and value('indicator') if value('DATE'): and value('indicator')

elif.....艾尔夫.....

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

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