while (hour < 24):
while (minute < 60):
while (second < 60):
second += 1
time.sleep(1)
print(str(hour) + ":" + str(minute) + ":" + str(second))
second = 0
minute += 1
minute = 0
hour += 1
To be honest I haven’t ran this code so I’m not 100% sure it works, but the logic worked in my head.
Basically:
You only keep incrementing the seconds and printing the time while the conditions of the 3 while loops are met: hour < 24, second < 60 and minute < 60. If the second < 60 condition isn’t met, it’ll break out of that loop and proceed to setting the second to 0 and incrementing the minute by 1.
If the minute < 60 condition isn’t met, it’ll break out of that loop and set the minute to 0 and increment the hour by 1.
I truly hope this helped you, and if I made any errors please let me know
Just to clarify what we are dealing with, is that a homework assignment? And is it allowed that we help you? Remember that your professor might see the help you have gotten
On your for loop (instead of what you have right now).
The range() function takes 2 parameters (it can technically take more but for now you just need the first two): start and end.
In a for loop, you can iterate over a sequence of numbers produced by the range() function.
The sequence will start at the number you give it as the start argument, and end one number before the end argument.
I’m just going to jump in here to what @yizuhi said.
range() is actually a constructor, meaning it creates a range object. It can take one, two, or three arguments.
The way for loops are implemented in Python is different than how they’re used in C-family languages.
for (int i = 0; i < 5; i++):
Here, i acts like a counter variable, increments by one each iteration, and once the condition i < 5 is true (which will happen after 5 iterations), the loop exits.
for i in range(5):
The following is not completely accurate; I’m describing it like so just to present an analogy. Here, you can think of i as a counter variable that starts at 0 (since only one argument was passed to range() here). i increases by 1 each iteration and has a maximum value of 4 (one less than 5, the argument passed to range()). This means that after the iteration where i = 4, the loop exits.
See more here (just read the middle section about range()).