How to record time? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

How to record time?

I’m making my first basic game with the little knowledge I have of python, and want to incorporate a feature that tells the user how much time it took them to complete the game, how would I go about doing this? Also I’d like to make a time trial mode, is there a way to stop the program as soon as a timer reaches zero?

4th Oct 2019, 10:38 PM
Advanced Incognizant
11 Answers
+ 1
So I analayzed your code and it just worked finde. The own problem was, you have to take start = time.time() delete it and type it before "while guess != rng" without indentations . Please contact me on solo learn to be little faster. I wanna really help you out
6th Oct 2019, 3:00 PM
William M
William M - avatar
+ 1
One way is to use perf_counter. You can see an example for how to set it up in this code (lines 1017 and 1037): https://code.sololearn.com/cLDkFpQaT06p/?ref=app
4th Oct 2019, 10:59 PM
HonFu
HonFu - avatar
+ 1
Simple: import time Start = time.time() User = input("") End = time.time() - Start # difference print("it took you", end, "seconds to type", User)
5th Oct 2019, 2:00 PM
William M
William M - avatar
+ 1
William M That worked! thank you so much you were a great help :)
6th Oct 2019, 3:02 PM
Advanced Incognizant
+ 1
No problem you are welcome any time
6th Oct 2019, 3:07 PM
William M
William M - avatar
0
William M This works, but it gives me 10+ decimal points when it prints, how can i shorten this to two decimals?
6th Oct 2019, 2:30 PM
Advanced Incognizant
0
import time Start = time.time() User = input("") End = time.time() - Start # difference print("it took you", round(End, 2), "seconds to type", User)
6th Oct 2019, 2:31 PM
William M
William M - avatar
0
Just changed in print (round(End,2)) to round it on two digits after the comma.you can also print it as whole number just delete the 2
6th Oct 2019, 2:32 PM
William M
William M - avatar
0
William M When i try this, i get an error ‘float’ object can not be interpreted as an integer
6th Oct 2019, 2:40 PM
Advanced Incognizant
0
Can you give me a link or a copy of your code. I posted the code after I tried it and it worked.
6th Oct 2019, 2:45 PM
William M
William M - avatar
0
William M https://repl.it/@Chromal/count This doesnt work on sololearn but works on the website link. The last line(line 103) is where i have round(end,2)
6th Oct 2019, 2:48 PM
Advanced Incognizant