0

I would like to ask if I can reduce many if statements in Python. I've had a problem with many if statements in my budget tracker program. The user for example get money every Month, he have expenses every Week and recurring expenses every Month. But he want to show for example savings for a day. That's many if statements to do every if. If he want to show savings for a day and other things are Month I need divide other things by days of the current Month, then I can count the savings for a day (savings = income - expenses - recurring expenses). I think you can understand me, if not ask me.Sorry if I writed something wrong here, my English isn't perfect.

phases = ['Day','Week','two weeks','three weeks','Month','three months','half a year','Year','two years','five years']
if self.combobox_value_savings == 'Day':
    if self.combobox_value_phase_income == 'Day':
        if self.combobox_value_phase_recurring == 'Day':
            if self.combobox_value_phase_expenses == 'Day':
                # everything the same -> nothing changes
            elif self.combobox_value_phase_expenses == 'Week':
                self.expenses /= 7
            elif self.combobox_value_phase_expenses == 'two weeks':
                self.expenses /= 14
            elif self.combobox_value_phase_expenses == 'three weeks':
                self.expenses /= 21
            # ...

Thanks in advance!

khelwood
  • 55,782
  • 14
  • 81
  • 108
albertinio
  • 13
  • 5
  • 3
    I'd build a mapping `factors = {"week": 1/7, "two weeks": 1/14, ...}` and then do `self.expenses *= factors.get(self.combobox_value_phase_recurring.lower(), 1)`. – jonrsharpe Jun 11 '20 at 08:41
  • @jonrsharpe But how I need to do this if for example 'self.combobox_value_savings == 'five years'' and 'self.combobox_value_phase_expenses == 'Day'' then expenses needs to be multiply from day to be five years and when it's reverse it needs to be divided from five years to a day. – albertinio Jun 11 '20 at 08:53
  • I don't think this is the same scenario with #60208. If `self.combobox_value_savings`, `self.combobox_value_phase_income`, `self.combobox_value_phase_recurring` and `self.combobox_value_phase_expenses` all can has each value in `phases`, there will be `len(phases)**4` conditions (here is `10**4 = 10000`). Also if each condition has different expenses factor, it will be hard to write logic, debug and maintain just with code logic. So maybe database (if this project has used one) or a data file such as csv, yaml, json etc. could be an option. – Fogmoon Jun 11 '20 at 10:27

2 Answers2

0

You can use below logic to decrease if ladder(do changes as your requirement).

score_map = {'Day':1,'Week':7,'two weeks':14,'three weeks':21,'Month':30}

# 1 option
for i in score_map:
    if self.combobox_value_phase_recurring == i:
        self.expenses /= score_map[i]
#2 option
self.expenses *= score_map.get(self.combobox_value_phase_recurring.lower(), 1)
Prashant Godhani
  • 337
  • 1
  • 4
  • 15
0

create a dictionary mapping for reducing your code:

dict_mapping = {'Day':1, 'Week': 1/7, 'two weeks':2/7 ,'three_weeks':3/7 } and so on for all the values

phases = ['Day','Week','two weeks','three weeks','Month','three months','half a year','Year','two years','five years']
if self.combobox_value_savings == 'Day':
    if self.combobox_value_phase_income == 'Day':
        if self.combobox_value_phase_recurring == 'Day'
             self.expenses *= dict_mapping[self.combobox_value_phase_recurring]
Abhishek Kulkarni
  • 1,747
  • 1
  • 6
  • 8