Skip to content
Sign upLog in
← Back to Community

How to add a timer in Python

Profile icon
Bookie0

Hello!

So I am making this sort of game where you have to make a sort of food. The way you make it is like this:
For example, if the user is asked to make a pizza, they have to type:
d c t (dough cheese tomato).

So then, they get money if they type the correct letters.

So my question is how to create a timer ⏱ that monitors how long it takes for the user to type those letters.

**The goal is for the timer to start when the input comes up, and ends once the user has clicked enter. **

Then, how much money they earn is based on how long it took. (But that will use an if statement, so i dont need help with that).

I hope you can help me, and thanks for taking time for checking this question!

Thank you very much in advance!

Answered by CodingCactus [earned 5 cycles]
View Answer
Voters
Profile icon
Diamond-R9R9
Profile icon
GarretThyCarrot
Profile icon
stubaduble
Profile icon
Bookie0
Comments
hotnewtop
Profile icon
CodingCactus

you need to import time, then set the time that it started at, and then minus that from the time that it ended (see code below)

import time timeStart = time.time() choice = input("Enter choice:\n") timeEnd= time.time() timeTaken = timeEnd - timeStart print("That took: " + str(timeTaken) + "seconds")
Profile icon
CodingCactus

@Bookie0
did it help?

Profile icon
NoelB33

That won’t work. Run the code.

@CodingCactus

Profile icon
Bookie0

Hey, I’m going to try it out rn but it seems pretty logic but thanks!

@CodingCactus

Profile icon
Bookie0

So i tried it out, and it gave me an error on last line asking if i meant == instead of = , so i did that, but now it just gives me false instead of a number:
4831443D-DD79-49C3-9C9B-FDBD97B8866B


@CodingCactus

Profile icon
CodingCactus

@NoelBryan
works for me

Profile icon
Bookie0

Oh wait never mind you wrote = but i think you meant + and it works now

@CodingCactus

Profile icon
CodingCactus

@Bookie0
yh, that was a typo, it should be a + as you are just concatenating

Profile icon
Bookie0

Ok yeah it worked. Thank you very much for your great help! :D

@CodingCactus

Profile icon
CodingCactus

@Bookie0
np

Profile icon
NoelB33

Told you it didn’t work.

@CodingCactus

Profile icon
CodingCactus

@NoelBryan
but it does

Profile icon
Bookie0

Now it does

@NoelBryan

Profile icon
CodingCactus

@NoelBryan
It was just an obvious typo, which you thought broke it or something

Profile icon
NoelB33

Except for the fact that your “obvious” type bookie0 didn’t recognize..............

Profile icon
CodingCactus

@NoelBryan
well, to me it looks like they recognised itimage

Profile icon
NoelB33

Then why did they comment?

@CodingCactus

Profile icon
Bookie0

Because before I wrote that there was a problem, but then I commented to

@CodingCactus
that actually I resolved it
@NoelBryan

Profile icon
CodingCactus

@NoelBryan
why are you focusing so heavily in this lol. Are you really bored or something?

Profile icon
Bookie0

Lol

@CodingCactus

Profile icon
Tsadema

@Bookie0
This escalated quickly xD

Profile icon
Bookie0

Yeah lol

@Tsadema

Profile icon
NoelB33

You can use the getKey module, and clear the screen and re-print it out every second, like

@CodingCactus
’s typing speed test. That is probably the only way because otherwise they would have to redo it every time you clear the screen and it would be impossible.

Profile icon
CodingCactus

@NoelBryan
that aint what they were asking tho

Profile icon
CodingCactus

@NoelBryan
I don't print the screen every second in my typing speed test either

Profile icon
NoelB33

Ohh, I thought they meant a timer displaying on the screen. The only way to do that is my awnser.

@CodingCactus

Profile icon
NoelB33

@CodingCactus

Please re-read my post. You don’t understand what I said.

Profile icon
CodingCactus

@NoelBryan
your post doesn't really make sense to me if i'm honest

Profile icon
Bookie0

Yeah, I didn’t mean a timer on screen, but thank you very much for your answer!

@NoelBryan

Profile icon
CodingCactus

@NoelBryan
Ummmm......

Profile icon
BobNeo

@NoelBryan
@CodingCactus

I stumbled upon this post is there a way to make one that displays the timer while having somebody put in input because I want to try something.

Profile icon
Bookie0

Well I’m not sure, maybe you can just print the timer and then put the input below.
Or maybe you can do if it’s a countdown like:

countdown = 100 while countdown < 100: print (countdown) countdown -= 1

But I don’t know if that’s what you’re asking for


@BobNeo

Profile icon
CodingCactus

@BobNeo
you can't have a clock counting down (being displayed) while waiting for an input, because an input will pause the program until something is input.

Profile icon
NoelB33

Use my solution. It works for that, that’s the only thing it works for

@BobNeo

Profile icon
BobNeo

Oh yeah, that seems like it would work.

@NoelBryan
@CodingCactus

However, is there any easier way without importing getKey

Profile icon
CodingCactus

@BobNeo

importing getkey isn't hard anyway, it is like importing time or any other library

Profile icon
CodingCactus

@NoelBryan
getkey still pauses the program like a normal input does

Profile icon
BobNeo

I know it’s just slow because repl.it takes awhile to import packages saying “importing getKey”

@CodingCactus

Profile icon
BobNeo

@CodingCactus

I think what
@NoelBryan
means is you keep track of what keys were pressed without pressing enter like in an array and every second you print out the time. That would work.

Profile icon
CodingCactus

@BobNeo
Yes, that is how getkey works, you don't need to press enter

But you can't print out the time every second if there is no key pressed, because the program will wait for a key to be pressed until it does anything