如何修复我的工作日,以在周六和周日输出特定信息。从周一到周五,人们都在工作,周六到周日是周末
How can I fix my weekdays, to output specific info on Saturdays and Sundays. From Mon to Fri, the people are at work and Sat to Sun is the weekend
这是模拟的一部分。我的今天作为当前纪元,它总是从 0 开始。我当前的代码如下:
is_weekend = current_day % 5 == 0 or current_day % 6 == 0
if is_weekend:
print("people are at home")
else:
print("people are at work")
for person in people:
if is_weekend:
person.location = "Home"
else:
person.location = "Work"
但是,当我进入计划的第二周工作时,周六他们在家,但周日他们又回到工作岗位。我以为使用 % 会起作用,但似乎有问题。
问题是我们有 7 days
而您检查 multiples of 5 and 6
。按照这种逻辑,10th day
将与 5th day
是同一工作日,但这是错误的 - 一周后是 12th day
。此外,0 % x returns 0
因此在使用模运算符时必须考虑到这一点。在我们的例子中,这无关紧要,因为我们不再检查 0
。
weekdays = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'];
for i in range(0, 14):
day_of_week = i % 7;
is_weekend = day_of_week == 5 or day_of_week == 6;
print('The', i, 'day is a', weekdays[day_of_week], '=> is weekend?', is_weekend)
您必须检查本周的 5
和 6
的剩余部分,因为一周有 7 天。
The 0 day is a Mon => is weekend? False
The 1 day is a Tue => is weekend? False
The 2 day is a Wed => is weekend? False
The 3 day is a Thu => is weekend? False
The 4 day is a Fri => is weekend? False
The 5 day is a Sat => is weekend? True
The 6 day is a Sun => is weekend? True
The 7 day is a Mon => is weekend? False
The 8 day is a Tue => is weekend? False
The 9 day is a Wed => is weekend? False
The 10 day is a Thu => is weekend? False
The 11 day is a Fri => is weekend? False
The 12 day is a Sat => is weekend? True
The 13 day is a Sun => is weekend? True
这就是模的工作原理(在本例中):
0 % 7 => 0 / 7 => remainder is 0
1 % 7 => 1 / 7 => remainder is 1
2 % 7 => 2 / 7 => remainder is 2
...
7 % 7 => 7 / 7 => remainder is 0
8 % 7 => 8 / 7 => remainder is 1
9 % 7 => 9 / 7 => remainder is 2
模运算符(在这种情况下)会告诉我们当我们将第一个数字一次又一次地除以 7 时还剩下多少......直到我们得到一个小于正确数字(即 7)的数字这将是模运算符的结果。
300 / 7 = 42.8571 ... 42.8571 / 7 = 6.1224
但是
300 % 7 = 6 <=> (int) (300 / 7) = 6
在没有得到浮点数的情况下,我们能得到多少?
它是 294
因为 42 * 7 = 294
、6 is missing to 300
和 6 is smaller than 7, therefore, remainder 6
这是模拟的一部分。我的今天作为当前纪元,它总是从 0 开始。我当前的代码如下:
is_weekend = current_day % 5 == 0 or current_day % 6 == 0
if is_weekend:
print("people are at home")
else:
print("people are at work")
for person in people:
if is_weekend:
person.location = "Home"
else:
person.location = "Work"
但是,当我进入计划的第二周工作时,周六他们在家,但周日他们又回到工作岗位。我以为使用 % 会起作用,但似乎有问题。
问题是我们有 7 days
而您检查 multiples of 5 and 6
。按照这种逻辑,10th day
将与 5th day
是同一工作日,但这是错误的 - 一周后是 12th day
。此外,0 % x returns 0
因此在使用模运算符时必须考虑到这一点。在我们的例子中,这无关紧要,因为我们不再检查 0
。
weekdays = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'];
for i in range(0, 14):
day_of_week = i % 7;
is_weekend = day_of_week == 5 or day_of_week == 6;
print('The', i, 'day is a', weekdays[day_of_week], '=> is weekend?', is_weekend)
您必须检查本周的 5
和 6
的剩余部分,因为一周有 7 天。
The 0 day is a Mon => is weekend? False
The 1 day is a Tue => is weekend? False
The 2 day is a Wed => is weekend? False
The 3 day is a Thu => is weekend? False
The 4 day is a Fri => is weekend? False
The 5 day is a Sat => is weekend? True
The 6 day is a Sun => is weekend? True
The 7 day is a Mon => is weekend? False
The 8 day is a Tue => is weekend? False
The 9 day is a Wed => is weekend? False
The 10 day is a Thu => is weekend? False
The 11 day is a Fri => is weekend? False
The 12 day is a Sat => is weekend? True
The 13 day is a Sun => is weekend? True
这就是模的工作原理(在本例中):
0 % 7 => 0 / 7 => remainder is 0
1 % 7 => 1 / 7 => remainder is 1
2 % 7 => 2 / 7 => remainder is 2
...
7 % 7 => 7 / 7 => remainder is 0
8 % 7 => 8 / 7 => remainder is 1
9 % 7 => 9 / 7 => remainder is 2
模运算符(在这种情况下)会告诉我们当我们将第一个数字一次又一次地除以 7 时还剩下多少......直到我们得到一个小于正确数字(即 7)的数字这将是模运算符的结果。
300 / 7 = 42.8571 ... 42.8571 / 7 = 6.1224
但是
300 % 7 = 6 <=> (int) (300 / 7) = 6
在没有得到浮点数的情况下,我们能得到多少?
它是 294
因为 42 * 7 = 294
、6 is missing to 300
和 6 is smaller than 7, therefore, remainder 6