# How to create a phython clock with 3 loops?

Hi,
I’m trying to make clock with python.
At first, I tried to do the following:

``````import time
import random

hour = random.randrange(0, 23)
minute = random.randrange(0, 60)
second = random.randrange(0, 60)

while (hour < 24):
second += 1
time.sleep(1)

if (second >= 60):
second = 0
minute += 1

if (minute >= 60):
minute = 0
hour += 1

print(str(hour) + ":" + str(minute) + ":" + str(second))

``````

It works, but It has to be with 3 while loops. Does anyone know how to do this?

Hi! Welcome to the forums I’m thinking you could do something like this:

``````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 `second`s 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 4 Likes

That works,
Now I need to have user input. I tried using a for statement.

``````import time

h = input("Enter hours:")
m = input("Enter minutes:")
s = input("Enter seconds:")

for(int h=0; h<24; h++):
for(int m=0; m<60; m++):
for(int s=0; s<60; s++):
print(str(h) + ":" + str(m) + ":" + str(s))
``````

I get syntax error using that for statement. What is wrong with the code now?

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

This is not homework. A friend of mine sent me some exercises he got from school, so I can try those exercises by myself.
I do it for fun 1 Like

That does explain why it looks like homework.

As for your error, the loop you wrote looks more like C syntax then python

you should use `range()`

4 Likes

I’m sorry to ask but where must `range()` go?

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.

1 Like

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()`).

2 Likes

Thanks for the help!
I got it working.

I learned a lot with this exercise btw 2 Likes