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
3
  • 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. Commented Dec 26, 2019 at 3:58
  • 3
    f"{month},Monthly" ? Commented Dec 26, 2019 at 3:58
  • Sorry I am fairly new in python. How do I exactly use that? f"{month},Monthly"? Commented Dec 26, 2019 at 4:06

1 Answer 1

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
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.