How to force countdays to calculate from a array

How to force countdays to calculate from a array from a range of 1-7 so lets say
its 21 it outputs tuesday if the input day is monday

Your code so far
currentdate = weekdaysarray[count]
def weekdays ( weekday ):
index = days.index( weekday )
return list( days[index:] + days )[:7]

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/101.0.4951.67 Safari/537.36

Challenge: Time Calculator

Link to the challenge:

I have a bit of trouble understanding what you want to do - if you speak “arrays” maybe an example with an array would be helpful

Though looking at your code, what you propably need is a “list-comprehension” - is a special Python syntax which creates a list with specific elements.
For example to create [1,2,3,4,5] you can use [i+1 for i in range(1,5)] → so you can use any operation for the item, while the “” can take any iterable object.

so lets say i have a list of weekdays
days = (‘Monday’, ‘Tuesday’, ‘Wednesday’, ‘Thursday’, ‘Friday’, ‘Saturday’, ‘Sunday’)
weekdaysarray = weekdays(day.title())
i get the weekday thats passed from calling add_time
def add_time(start, duration,day=None):
i count the number of days from between start and duration in days and i want that specific weekday
the problem is for example if the weekday is more than 6 , it freaks out
so is there a way to make 20-> 5 21 → 6 22-> 0 etc

Yeah, the modulo-operator, which gives the residual for integer-divisions:
20%7 == 5

so like this?
countdaystemp = countdays%7 if countdays> 7 else countdays
currentdate = weekdaysarray[countdaystemp]

You don’t even need the if-else :wink:
The modulo(x) basically creates a “circle” of numbers from 0 to (x-1) → so the modulo(7) works like this:

0%7 == 0
1%7 == 1
2%7 == 2
3%7 == 3
4%7 == 4
5%7 == 5
6%7 == 6
7%7 == 0
8%7 == 1
9%7 == 2
1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.