from datetime import datetime
platenumber = input("Enter the license plate number:")
start = input("Press enter to start.")
starttime =(datetime.now())
stop = input("Press enter to stop.")
stoptime =(datetime.now())
dist1 = 0
dist2 = input("Enter the distance of the road in metres:")
time = ((stoptime - starttime).total_seconds())
print((time),"is the time in seconds.")
distance = int(dist2) - dist1
print((distance),"is the the distance in metres.")
speed = float(distance)//time
print((speed),"is the speed of the vehicle in m/s.")
I want to restart the program after using it so that I can check the speed of the vehicle more than one time.
Please help me finish the code so that I can restart the code with itself and check the speed of more than one vehicle.
Use a while loop.
while True:
# Do your code
At the end:
again = input("Do this again?")
again = again.lower()
if again.startswith('n'):
break # Get out of the loop
Related
I created a small app where the user can add number of instances and then add arbitrary numbers, making it all end up in a pandas df. However, attempting to challenge the user doing aforementioned process, the user ends up being able to complete the process of adding values. This should stop abruptly, if time deadline is not met.
Try run the code yourself:
import time
import pandas as pd
# define the countdown func.
def countdown(t):
while t:
mins, secs = divmod(t, 60)
timer = '{:02d}:{:02d}'.format(mins, secs)
print(timer, end="\r")
a = int(input("Enter number of instances: "))
test3 = []
i = 0
while i < a:
pl = int(input("Enter arbitrary integer: "))
test3.append(pl)
i += 1
print(list(test3))
DF1 = pd.DataFrame(test3)
return DF1
time.sleep(1)
t -= 1
print('Game over')
# input time in seconds
t = input("Enter the time in seconds: ")
# function call
print(countdown(int(t)))
I'd suspect that I am missing an if-statement, but that is potentially what I need help doing or...?
Any help is appreciated...thx
EDIT:
import pandas as pd
from threading import Timer
timeout = 8
t = Timer(timeout, print, ['Game over'])
t.start()
prompt = "You have %d seconds to choose the correct answer...\n" % timeout
answer = input(prompt)
a=int(input("Enter number of instances: "))
test3=[]
i=0
while i < a:
pl=int(input("Enter arbitrary integer "))
test3.append(pl)
i+=1
print(list(test3))
DF1 = pd.DataFrame(test3)
print(DF1)
t.cancel()
import time
print("The timer on this project Will start")
#Ask To Begin
start = input('Would you like to begin learning now? (yes / no):')
if start == 'yes':
timeloop = True
#variable to keep the time
Sec = 0
Min = 0
#Begin process
timeloop = start
while timeloop:
Sec +=1
print(str(Min) + " Mins " + str(Sec) + " Sec ")
time.sleep(1)
if Sec == 60:
Sec = 0
Min +=1
print(str(Min) + " Minute ")
This is my timer program so far but I am not sure How I can get it to stop once it starts in the command prompt?. I want to be able to press X and have the code pause. Or perhaps press Y and then it resumes, and Z just closes the program all together, but I have no idea as to how.
time.sleep actually stop the execution of the current process the code is running on. If you want to keep time and still make the process responsive, you may want to use a thread to keep time and install the keyboard package to use keyboard.is_pressed()!
This article is really good:
https://realpython.com/intro-to-python-threading/#what-is-a-thread
So I have a code I'm working on to automatically play my alarms at the times I specify. It also is playing some city sounds to help me sleep and while playing those sounds I can't get the while loop to end to have the alarm sound to play.
I tried to bring the while loop into the alarm function I've created but that didn't work but I'm fairly certain it has something to do with my while loops. But I'm thinking a fresh pair of eyes might do this code some good.
'''
Title: This is an alarm that perfectly lines up with my sleep schedule
Author: Riley Carpenter
'''
from pygame import mixer
import time
import os
import sys
import random
snoozeorstop = " "
Currenttime = time.ctime()
Hours1 = int(Currenttime[11:13])
Minutes1 = int(Currenttime[14:16])
Seconds = int(Currenttime[17:19])
optionalsongs = ["Pink Floyd Time.wav","Alarm2.wav","Alarm3.wav","Alarm4.wav","Alarm5.wav","Alarm6.wav","Alarm7.wav","Alarm8.wav","Alarm9.wav","Alarm10.wav","Alarm11.wav"]
phrases = ["Wake up Riley!!!!","It's time to wake up it's time to wake up","HEEEEYYY WAKE UP","RILEY RILEY RILEY WAKE UP","1 2 3 4 5 6 7 8 9 it is time to wake up","Riley more alarms are to come UNLESS you get up","OH WHEN SHALL I SEE JESUS you wanna not hear this again? Wake up","I'm so tired of telling you to wake up just wake up","A friend of the devil is somehow who doesn't wake up","Babe babe bae wake up"]
def playsound(soundfile):
mixer.init()
mixer.music.load(soundfile)
mixer.music.play(-1)
def stopsound():
mixer.music.stop()
def alarm(hour,minute):
print("")
print("")
print(random.choice(phrases))
if Hours1 == hour and Minutes1 == minute:
stopsound()
playsound(random.choice(optionalsongs))
print("")
snoozeorstop = input("Do you want to stop the song? ")
if snoozeorstop == "stop":
stopsound()
citysounds = input("Do you want to play the soothing sounds of the city? ")
if citysounds == "y" or citysounds == "Yes" or citysounds == "Y" or citysounds == "yes":
playsound("Citys Night ambience sounds.wav")
else:
playsound("Beginningsound.wav")
amount = 0
print("This is how many second has passed since this alarm was turned on")
while Hours1 != 5 and Minutes1 != 0:
Currenttime = time.ctime()
print(amount)
Hours1 = int(Currenttime[11:13])
Minutes1 = int(Currenttime[14:16])
Seconds = int(Currenttime[17:19])
amount += 1
time.sleep(1)
alarm(5,00)
alarm(5,5)
alarm(5,10)
alarm(5,15)
alarm(5,20)
alarm(5,25)
alarm(5,30)
alarm(5,35)
alarm(5,40)
alarm(5,45)
alarm(5,50)
alarm(5,55)
alarm(6,00)
alarm(6,5)
alarm(6,10)
alarm(6,15)
alarm(6,20)
alarm(6,25)
Since the while loop is central to your program, why not write that piece of code first. When that works as intended you can add songlists etc.
As for checking for the time when the alarm should go off you might want to check out the datetime module which makes time calculations much easier. For example:
import datetime, time
start = datetime.datetime.now()
alarm = start + datetime.timedelta(seconds=5) # Set alarm time 5 seconds from now
while True:
now = datetime.datetime.now() # When is this?
print(now.strftime("%H:%M:%S")) # Print human readable time
if now > alarm: # Have I passed the alarm time?
print('Alarm') # Sound the alarm
break # Leave this loop
time.sleep(1)
I want to create a game where the idea is to spam as much as possible in the time limit: 10 seconds
import time
import random
print("Spamming race")
print("*************")
time.sleep(10)
print("You must spam the number '1'.")
time.sleep(3)
print("Ready")
time.sleep(1)
print("Set")
no = (0.25,0.5,0.7,1,1.25,1.5,1.7,2,2.25,2.5,2.75,3)
number = random.choice(no)
time.sleep(number)
print("Go!")
max_time = 1
t = 31
start_time = time.time()
g = input()
if time.time - start_time > max_time > t: #where the problem is but I don't know why
distance = g.count('1')
print("And he crosses the line with a distance of ",distance)
It says the problem is on line 23 but I can't see what is the problem can someone help me?
As #Rawing pointed out, you forget to call time.time:
if time.time()-start_time>max_time>t:
distance=g.count('1')
print("And he crosses the line with a distance of ",distance)
Rawing is correct. To elaborate, use time.time() instead of time.time
I think what you want for that line is:
if (time.time()-start_time)>t:
I am trying to create a stopwatch that starts and stops through the user pressing the enter. Once to start and again to stop. The start works perfectly but the stopping section is not working. I've tried creating a variable called stop that is like so:
stop = input("stop?")
But it's still not working.
import time
def Watch():
a = 0
hours = 0
while a < 1:
for minutes in range(0, 60):
for seconds in range(0, 60):
time.sleep(1)
print(hours,":", minutes,":", seconds)
hours = hours + 1
def whiles():
if start == "":
Watch()
if start == "":
return Watch()
def whiltr():
while Watch == True:
stop = input("Stop?")
#Ask the user to start/stop stopwatch
print ("To calculate your speed, we must first find out the time that you have taken to drive from sensor a to sensor b, consequetively for six drivers.")
start = input("Start?")
start = input("Stop")
whiles()
Perhaps all you need is something simple like:
import time
input('Start')
start = time.time()
input('Stop')
end = time.time()
print('{} seconds elapsed'.format(end-start))
Should probably use the time function instead of
def Watch():