Gusto - 1 year ago 135

Python Question

My task is to deﬁne a function

`weekdays(weekday)`

`>>> weekdays('Wednesday')`

['Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday', 'Monday', 'Tuesday']

So far I've come up with this one:

`def weekdays(weekday):`

days = ('Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday',

'Sunday')

result = ""

for day in days:

if day == weekday:

result += day

return result

But this prints the input day only:

`>>> weekdays("Sunday")`

'Sunday'

What am I doing wrong?

Answer Source

```
def weekdays(day):
days = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']
i=days.index(day) # get the index of the selected day
d1=days[i:] #get the list from an including this index
d1.extend(days[:i]) # append the list form the beginning to this index
return d1
```

And if you want to test that it works:

```
def test_weekdays():
days = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']
for day in days:
print weekdays(day)
```