搜尋此網誌

2026年6月24日星期三

While loops

innocuous: not intended or likely to offend or upset anyone

tame something to make an animal, bird, etc. not afraid of people and used to living with them

The datetime class in Python is part of the datetime module and is used to represent both date and time together, with optional time zone support. It allows you to create, manipulate, and format date-time objects with precision, making it essential for scheduling, logging, and time-based calculations.

In Python, the % symbol is the modulus operator. It returns the remainder when one number is divided by another.

In Python, pass is a placeholder statement that literally means “do nothing.”


from datetime import datetime

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

  • datetime.now().second → gets the current second (0–59).

  • + 2 → adds 2 seconds to the current time.

  • % 60 → ensures the value wraps around correctly (e.g., if current second is 59, 59 + 2 = 61, and 61 % 60 = 1).

So wait_until is always two seconds ahead of the current second.

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

  • This loop keeps checking the current second.

  • As long as it’s not equal to wait_until, it prints "Still waiting!".

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

    Once the current second matches wait_until, the loop ends.
    It prints a confirmation message showing the target second.

In Python, the f in print(f"...") is shorthand for an f‑string, also called a formatted string literal. It lets you embed variables or expressions directly inside a string using curly braces {}.

name = "Tsz"
age = 25
print(f"My name is {name} and I am {age} years old.")

Output:
My name is Tsz and I am 25 years old.

A literal in Python (and in programming generally) is a fixed value that you write directly into your code. It represents itself, not a variable or expression. Examples are integer, float, string, and boolean.

from datetime import datetime

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

while True:
    if datetime.now().second == wait_until:
        print(f'We are at {wait_until} seconds!')
        break

wait_until is set to 2 seconds ahead of the current second (wrapped with % 60).

The while True: loop runs indefinitely.

while → starts a loop that repeats as long as its condition is True.
True → is a Boolean literal that always evaluates to true.
So while True: means “keep looping forever” because the condition never becomes false on its own.

Inside, you check if datetime.now().second == wait_until.

When the condition is true, it prints the message and break exits the loop.


from datetime import datetime

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

while True:   # outer loop runs forever
    while datetime.now().second == wait_until:   # inner loop runs only when condition is true
        print(f'We are at {wait_until} seconds!')
        break

When the condition is true, it prints the message once and then break exits the inner loop.
But notice: break only stops the inner loop, not the outer while True: loop.
That means the outer loop will continue running forever, checking again and again.


from datetime import datetime wait_until = (datetime.now().second + 2) % 60 while datetime.now().second != wait_until: continue print('Still waiting!') print(f'We are at {wait_until} seconds!')

wait_until is set to 2 seconds ahead of the current second (wrapped with % 60).
The while loop runs until the current second equals wait_until.

Inside the loop you wrote:
continue print('Still waiting!')

continue immediately jumps back to the start of the loop, skipping everything after it.
That means the print('Still waiting!') line is never executed.


from datetime import datetime wait_until = (datetime.now().second + 2) % 60 while True: if datetime.now().second < wait_until: continue break print(f'We are at {wait_until} seconds!')

Target second
wait_until is set to 2 seconds ahead of the current second, wrapped with % 60.

Infinite loop (while True:)
The loop runs continuously until you explicitly break out.

Condition check
If the current second is less than wait_until, the loop executes continue.
continue skips the rest of the loop body and goes back to the top, so nothing else happens.

Break
As soon as the current second is not less than wait_until (i.e., equal or greater), the loop executes break.
This exits the loop completely.

Final print
After breaking, the program prints the message showing the target second.

Suppose the current second is 18.
wait_until = (18 + 2) % 60 = 20.
While the clock shows seconds less than 20 (18, 19), the loop keeps spinning.
As soon as the clock reaches 20 or higher, the loop breaks.

Microsoft Copilot

沒有留言:

發佈留言