shahab96 wrote:try making a list in which you put all the days. after each day has passed acces the next thing in the list and when it is sunday set the list to go back to its beginning. I have seen code for that but I cant remember where it is.
Making a list of days is simple enough:
Code: Select all
week = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', Saturday', 'Sunday']
If you want to just iterate through a list, you can do something like this:
Code: Select all
for day in week:
# This code will run once for every item in the 'week' list, with 'day' set to each item in turn
if day == 'Monday':
# This code will only run if it's Monday...
if day in ['Saturday', 'Sunday']:
# This code will only run if it's a weekend...
- however, the problem with a 'for' loop is that it'll run through every item in the list once, and then move on - it won't keep going forever... so you'll only see one Monday, one Wednesday and so on.
If you want something which keeps going forever (or at least until some condition is met), you want a while loop:
Code: Select all
done = False
while (done == False):
# This code will be run over and over and over until the 'done' flag is set to true, then it'll stop looping.
...
if (won_girl and saved_world):
done = True
Of course, this doesn't help you get through your week. One way you could do it is with list manipulation. For example, the split operator gives you a sub-section of a list, and it's done like this:
Code: Select all
a = myList[2:] # Get a sub-list which contains every item after the second one
b = myList[:6] # Get a sub-list which contains every item up to the sixth one
So you could loop through your week like this:
Code: Select all
# Copy-and-Pasted from the Python interpreter to demonstrate the approach:
>>> week = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']
>>> week
['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']
>>> week = week[1:] + week[:1]
>>> week
['Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday', 'Monday']
>>> week = week[1:] + week[:1]
>>> week
['Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday', 'Monday', 'Tuesday']
Which you could implement something like this:
Code: Select all
week = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']
done = False
while (done == False):
day = week[0] # Get the first item in the 'week' list
# Do stuff for that day
...
# shuffle first item in list off to the end of the list.
week = week[1:] + week[:1]
if (won_girl and saved_world):
done = True
However, there's probably a much easier way to do it. The modulus operator (%), when given two numbers, will give you the
remainder of dividing the first number by the second:
Code: Select all
>>> 5 % 2
1
>>> 10 % 2
0
>>> 3 % 2
1
>>> 4 % 7
4
>>> 13 % 7
6
>>> 7 % 7
0
So if you just keep incrementing the day by 1 (as is done in the DSE demo, for example) then you can just take the modulus of the day by 7, and you'll get a number between 0 and 6 - which you can then use as the index into the list of days:
Code: Select all
week = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']
done = False
day = 0
while (done == False):
dayNumber = day % 7
dayName = week[dayNumber]
# Do stuff for that day
...
# Advance week on one day
day += 1
if (won_girl and saved_world):
done = True