Printing the time between 2 points in a program in Python [duplicate] - python

This question already has answers here:
How do I get the current time?
(54 answers)
Closed 6 years ago.
I would like to know a way to time between two points in a program. In my situation I will ask the user 10 questions and after display the time it took for them to answer the question (example code below). How would i do this through something like import time ?
Example code:
timer.start
question1 = input("What is your favorite game ?")
timer.end
print(timer.time)
^
The timer.x thing is going to be replaced with your suggestions.

import time
s=time.time()
question1 = input("What is your favorite game ?")
e=time.time()
print(e-s)
time.time() Returns the time in seconds since the epoch as a floating point number.

How about this?
from datetime import datetime
start = datetime.now()
question1 = input("What is your favorite game ?")
end = datetime.now()
print(str(end - start))

Related

Python how to freeze program? [duplicate]

This question already has an answer here:
How to start/stop a Python function within a time period (ex. from 10 am to 12:30pm)?
(1 answer)
Closed 2 years ago.
Note: read my comments on the provided answer, it doesn't work
I have a python program which runs every 5 seconds like this:
def main():
start_time = time.time()
while True:
if not execute_data():
break
time.sleep(5.0 - ((time.time() - start_time) % 5.0))
The question is how can I make it run from 7:00 to 23:00 only? I don't want to use my computer resources at times where I'm sure my program won't be helpful...
You can use datetime.datetime.strptime() and datetime.datetime.strftime():
from datetime import datetime
import time
def str_to_time(string):
return datetime.strptime(string, "%H:%M")
def main():
start = '7:00'
end = '23:00'
while True:
now = str_to_time(datetime.now().strftime("%H:%M"))
if str_to_time(end) > now > str_to_time(start) or str_to_time(end) < now < str_to_time(start):
print('Working...')
time.sleep(5)
main()

How to make Python Chatbot to call specific function?

I made chatbot in Python with a help of Keras lib. My chatbot works great but now I want to add a feature so that it can tell what day, time, weather is it, for example:
me: what time is it?
bot: its 12:35
Data that I used is csv format where features are questions and answers are labels.
My model is predicting answer based on user question input.
For example, for this set of questions:
import pandas as pd
list1 = ["what's the time", "whats the time?", "what time is it?",
"today is what day?", "what day is today", "what day is it?",
"what is the temperature", "how is the weather?"]
list2 = ["it is time.time()", "it is time.time()", "it is time.time()",
"today is get_day()", "today is get_day()", "today is get_day()",
"the weather is get_weather()", "the weather is get_weather()"]
df = pd.DataFrame()
df["question"] = list1
df["answer"] = list2
question answer
0 what's the time it is time.time()
1 whats the time? it is time.time()
2 what time is it? it is time.time()
3 today is what day? today is get_day()
4 what day is today today is get_day()
5 what day is it? today is get_day()
6 what is the temperature the weather is get_weather()
7 how is the weather? the weather is get_weather()
The answer / label should be: "It is " + time.time()
But I can't pass "It is " + time.time() as a label because its not string or numeric value.
I know that i can get time in Python like time.time() but how can I add that function to be my answer. My answers need to be numeric values (because I'm passing them into ML model) so I cant write "time is"+time.time(). Is there a way to do this, to store function in json file or csv file and make ML model. I can change my data to be JSON format if that helps.
PS
I'm planning to make a lot of functions that will be predicted so simple solutions such as:
if answer == 'time':
print(time.time())
Does not work for me. I would like to automate this

Implementing a timer(countdown) that runs pararell with the game logic [duplicate]

This question already has answers here:
How to set time limit on raw_input
(7 answers)
Closed 6 years ago.
I have created a mathematical quiz game that prints and equation to the user like, 5 + 3 = ?, and waits for the result. If the answer is right the user wins if not the user loses. I want to extend the game and add a feature that places a time limit of 3 seconds for the user to answer, if he don't the he loses.
At the beggining I tried using the time module and the time.sleep() function but this also delayed the whole program's logic.
Here is an idea with pseudo code:
if (answer = wrong || time = 0)
lost...
if you want to check if the user took to long when he answered you can use the time module to calculate the diffrence:
start_time = time.time()
show_question()
answer = get_answer()
end_time = time.time()
if (answer = wrong || end_time - start_time > 3)
lose()
if you want the user to loose when 3 seconds as passed (without waiting for them to input a answer) you will have to use threading, like this:
timer = threading.Timer(3, lose)
show_question()
answer = get_answer()
timer.cancel()
if (answer = wrong)
lose()

How to set a pop up time limit in python? [duplicate]

This question already has answers here:
How to set time limit on raw_input
(7 answers)
Closed 9 years ago.
I want to have a time limit in order to enter the input in the following code. In other words, there should be a timer tracking the time and if it exceeds the limit, the code should print out a message like "Game over" automatically without hitting any key. it is a sort of pop-up.
def human(player, panel):
print print_panel(panel)
print 'Your Turn! , Hint: "23" means go to row No.2 column No.3/nYou got 1 min to move.'
start_time = time.time()
end_time = start_time + 60
while True :
move = raw_input('> ')
if move and check(int(move), player, panel):
return int(move)
else:
if (time.time() < end_time):
print 'Wrong move >> please try again.'
else:
print "Game over"
return panel, score(BLACK, panel)
break
the other question is almost the same but the answer is not what I am looking for. I want the code to return a message when the time is over without hitting "ENTER".
The simplest way is to use the curses module. You'll want to set nodelay(1), and poll for input. http://docs.python.org/2/howto/curses.html#user-input

I have no idea what is wrong with this code? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
I'm new to python, I can't tell if this will work or not
import time
from sys import stdout
varthing = 1
while varthing == 1:
time.sleep(1)
checker = time.strftime("\r%b, %d %H:%M:%S", time.localtime())
print checker,
stdout.flush()
if checker == "Dec, 25 00:00:00" :
print "It's Christmas"
raw_input("Enter anything to close\n")
varthing = 0
I don't see anything wrong. It is a clock the notifies you when Christmas is.
Your strftime format starts with \r. Why? The string you are testing against in the if statement will never match because it doesn't start with \r.
time.sleep(1) is not guaranteed to sleep for exactly one second. It might sleep longer and you'll miss the one-second window in which checker would match the string you're testing against.
If you don't need prints every second, this will do the trick:
import datetime, time
target_date = datetime.datetime(2011, 12, 25)
time_left = target_date - datetime.datetime.now()
time.sleep(time_left.total_seconds())
print "It's Christmas"

Categories

Resources