0

How do I use a variable inside an if statement in python? For example, month_report contains:

October
November
December
January

and cycle_report contains:

October,Monthly
November,Monthly
November,Quarterly
January,Monthly

Below is how I would think variable can be used.

mcount = 0
qcount = 0
ncount = 0

for month in month_report:
    for x in cycle_report:
        if x == "{month},Monthly":
            mcount += 1
        elif x == "{month},Quarterly":
            qcount += 1
        else:
            ncount += 1
pppery
  • 3,731
  • 22
  • 33
  • 46
Ykaly
  • 3
  • 1
  • Is `cycle_report` map? If so, then it would be `cycle_report[month]`, as value would be report. If full `"{month},Monthly"` is key, then you would have to format it before checking. – EnterSB Dec 26 '19 at 03:58
  • 3
    `f"{month},Monthly"` ? – Austin Dec 26 '19 at 03:58
  • Sorry I am fairly new in python. How do I exactly use that? ```f"{month},Monthly"```? – Ykaly Dec 26 '19 at 04:06

1 Answers1

0

I'm not sure about your data structure, but considering they are list of strings:

In [1]: month_report = ['October', 'November']

In [2]: cycle_report = ['October, monthly', 'November, monthly']

In [3]: for month in month_report:
   ...:     for cycle in cycle_report:
   ...:         if month + ', monthly' == cycle:
   ...:             print('yes')
   ...:
yes
yes
Osman Mamun
  • 2,864
  • 1
  • 16
  • 22