【发布时间】:2019-06-21 13:22:15
【问题描述】:
我有这段代码,它打印从 0 到 24 的数字,当时间等于 8、16、24 时,它打印你需要休息,现在关键是时间是 8、16 和 24 时打印时间和语句'8 你需要休息一下',而且在它迭代代码之后,在时间和语句下面它再次打印时间,你能解释一下如何避免这种情况吗?
time=0
while time!=25:
if time%8==0 and time!=0:
print (time,'you need to take a break')
if time == 25:
time=0
print (time)
time+=1
This is the result i get.
0
1
2
3
4
5
6
7
8 you need to take a break
8
9
10
11
12
13
14
15
16 you need to take a break
16
17
18
19
20
21
22
23
24 you need to take a break
24
And this is want i want to get
0
1
2
3
4
5
6
7
8 you need to take a break
9
10
11
12
13
14
15
16 you need to take a break
17
18
19
20
21
22
23
24 you need to take a break
【问题讨论】:
-
将
else与您的if一起使用? -
仅供参考:
if time == 25:处的死代码因为您正在使用time != 25并且增量始终为1
标签: python python-3.x function loops if-statement