0

I have a code where I pass a value in whichweek and then it returns the values of dayfand dayt I am using the if statement but it's pretty long and it doesn't feel "pythonic" at all. Is there any elegant way to write this?

here is the way I do it:

if whichweek == 5:
    dayf = 0
    dayt = 32
elif whichweek == 1:
    dayf = 0
    dayt = 8
elif whichweek == 2:
    dayf = 7
    dayt = 15
elif whichweek == 3:
    dayf = 14
    dayt = 22
elif whichweek == 4:
    dayf = 21
    dayt = 29

Thanks for help.

Steven G
  • 16,244
  • 8
  • 53
  • 77

4 Answers4

3
weeks = {1: (0, 8)}
try:
    dayf = weeks[1][0]
    dayt = weeks[1][1]
except (KeyError, IndexError):
    pass
Laszlowaty
  • 1,295
  • 2
  • 11
  • 19
  • This is the only answer so far that actually replicates the behavior defined by OP. Namely, `dayf` and `dayt` are not defined if whichweek is not in the dictionary. – Logan Byers Oct 05 '16 at 12:24
1

Create a dictionary to map the value whichweek as key to corresponding values of dayf and dayt. Even better to make a call to this dict using a function (I personally love these simple and task oriented functions).

Below is the sample code:

def get_values(key):
    return {
         1: (0, 8),
         2: (7, 15),
         3: (14, 22),
         4: (21, 29)
         5: (0, 32),
     }.get(key, (None, None))

dayf, dayt = get_values(5)
# Value of 'dayf' = 0 and 'dayt' = 32 

dayf, dayt = get_values(9)
# Since '9' is not the valid key; 'dayf' = None and 'dayt' = None

In case you do not want to set any value to dayf and dayt if whichweek is having unknown values (as my above solution is setting dayf and dayt as None in this scenario), slightly updated version of the above code will be:

def get_values(key):
    return {
        1: (0, 8),
        2: (7, 15),
        3: (14, 22),
        4: (21, 29)
        5: (0, 32),
    }[key]

try:
    dayf, dayt = get_values(5)
except KeyError:
    pass
Moinuddin Quadri
  • 46,825
  • 13
  • 96
  • 126
1

I would use a dictionary:

days = {
    5: (0, 32),
    1: (0, 8),
    2: (7, 15),
    3: (14, 22),
    4: (21, 29)
}

dayf, dayt = days[whichweek]
maillard
  • 680
  • 1
  • 14
  • 27
  • Nice! What will happen, if `whichweek` is not in the dictionary? :) – Laszlowaty Oct 05 '16 at 12:20
  • Your code will throw an error if `whichweek` isn't a key in `days`. Better to use `days.get(whichweek)` and check if None or wrap the bracket access call in a try-except block. – Logan Byers Oct 05 '16 at 12:21
1

Yeah, there are few ones to make it more pythonic, I'd use a dictionary packing all the data, ie:

import random

if __name__ == "__main__":
    random.seed(1)

    week_data = {
        1: (0, 32),
        2: (0, 8),
        3: (7, 15),
        4: (14, 22),
        5: (21, 29)
    }

    for i in range(10):
        whichweek = random.randint(1, 5)
        dayf, dayt = week_data[whichweek]
        print whichweek, dayf, dayt

If you want to handle errors, you can use something like this:

import random

if __name__ == "__main__":
    random.seed(1)

    week_data = {
        1: (0, 32),
        2: (0, 8),
        3: (7, 15),
        4: (14, 22),
        5: (21, 29)
    }

    for i in range(10):
        whichweek = random.randint(1, 10)
        res = week_data.get(whichweek, None)

        if res is None:
            print("Error: {0} is not in week_data".format(whichweek))
        else:
            dayf, dayt = res
            print whichweek, dayf, dayt

Finally, if you want to avoid that conditional check, return always values like this:

import random

if __name__ == "__main__":
    random.seed(1)

    week_data = {
        1: (0, 32),
        2: (0, 8),
        3: (7, 15),
        4: (14, 22),
        5: (21, 29)
    }

    for i in range(10):
        whichweek = random.randint(1, 10)
        dayf, dayt = week_data.get(whichweek, (None, None))
        print whichweek, dayf, dayt
BPL
  • 9,632
  • 9
  • 59
  • 117
  • Your code will throw an error if whichweek isn't a key in days. Better to use days.get(whichweek) and check if None or wrap the bracket access call in a try-except block. – Logan Byers Oct 05 '16 at 12:22