搜尋此網誌

2026年7月22日星期三

Chapter Quiz

wait_until = datetime.now().second + 2

while datetime.now().second != wait_until:

    print('Still waiting!')

print(f'We are at {wait_until} seconds!')


  • datetime.now().second gives you the current second (0–59) of the system clock.

  • You add 2 to it, so if the current second is 15, then wait_until becomes 17.

  • This means you want to wait until the clock reaches 17 seconds.


while datetime.now().second != wait_until: print('Still waiting!')

  • This is a loop that keeps checking the current second.

  • As long as the current second is not equal to wait_until, it prints "Still waiting!".

  • Example: If the current second is 15, and wait_until is 17, it will keep printing "Still waiting!" until the clock reaches 17 seconds.

Problem here: If wait_until goes beyond 59 (e.g., current second = 58 → wait_until = 60), the value 60 doesn’t exist in the clock (seconds only go 0–59). That means the loop will never end.


print(f'We are at {wait_until} seconds!')

Once the loop ends (meaning the current second equals wait_until), this line runs.

It prints a message like:

We are at 17 seconds!

Example Run:

Suppose the current second is 15:

  1. wait_until = 15 + 2 = 17

  2. While the clock is at 15 or 16, it prints "Still waiting!".

  3. When the clock hits 17, the loop ends.

  4. It prints "We are at 17 seconds!".


Your code works fine if wait_until stays within 0–59. But if the current second is 59, then wait_until = 61, which will never happen. To fix this, you should use modulo (% 60) to wrap around:

wait_until = (datetime.now().second + 2) % 60

That way, if the current second is 59, wait_until becomes 1 (the next minute’s first second), and the loop will still work.

['Monty Python' if n % 6 == 0 
 else 'Python' if n % 3 == 0 
 else 'Monty' if n % 2 == 0 
 else n 
 for n in range(1, 10)]

Step 1: Understanding the structure

  • This is a list comprehension that loops n from 1 to 9 (range(1, 10)).

  • For each n, it checks conditions in order:

    1. If divisible by 6 → 'Monty Python'

    2. Else if divisible by 3 → 'Python'

    3. Else if divisible by 2 → 'Monty'

    4. Else → just the number n


Step 2: Condition priority

The conditions are evaluated top to bottom:

  • % 6 == 0 has highest priority.

  • If that fails, % 3 == 0 is checked.

  • If that fails, % 2 == 0 is checked.

  • If all fail, return the number itself.


Step 4: Final output

So the list becomes:

[1, 'Monty', 'Python', 'Monty', 5, 'Monty Python', 7, 'Monty', 'Python']


In Python, break is used inside loops to immediately exit the loop, no matter what the loop condition is. Let’s go step by step:

How break works

  • Normally, a while or for loop keeps running until its condition becomes false.

  • If you use break, the loop stops right away—even if the condition is still true.

  • After breaking, the program continues with the code that comes after the loop.


Example with while

n = 1
while n < 10:
    print(n)
    if n == 5:
        break   # exit loop when n == 5
    n += 1

print("Loop ended")

Output:

1
2
3
4
5
Loop ended

range(1, 9) in Python creates a sequence of integers starting at 1 and stopping before 9.


Microsoft Copilot

沒有留言:

發佈留言