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:
Related
This is my code and every time I run it, it just goes back to 0 so it never adds it to the bank, i know it’s a stupid question but i’d appreciate some help. Thank you!
import random as r
# get the start time
st = time.time()
bank=0
def work ():
z=r.randint(1000,8000)
global bank
bank=bank+z
jobs=[]
y=r.choice(jobs)
e=f"You worked as {y} and made {z} and you now have {bank} in your account"
return e
print(work())
time.sleep(1)
# get the end time
et = time.time()
# get the execution time
elapsed_time = et - st
print('Execution time:', elapsed_time, 'seconds')```
How would I add it back to the original bank variable?
Frankly speaking, I don't quite understand what you mean, but I guess you may want to write like this
import random as r
import time
# get the start time
st = time.time()
bank=0
while True:
def work ():
z=r.randint(1000,8000)
global bank
bank=bank+z
jobs=['testjob']
y=r.choice(jobs)
e=f"You worked as {y} and made {z} and you now have {bank} in your account"
return e
print(work())
time.sleep(1)
# get the end time
et = time.time()
# get the execution time
elapsed_time = et - st
print('Execution time:', elapsed_time, 'seconds')
stopflag = input('if you want work again ,plsea send 1 : ')
if stopflag != '1' :
break
import time
import os
T = int(input("Enter desired time for the timer - "))
t = time.localtime(time.time())
def Timer():
while ((t.tm_sec) != T+(t.tm_sec)):
return t
else:
os.system("start C:/Users/Public/Music/Sample Music")
Timer()
I have been working on this timer and can't get it to work. Basically, I want it to play the song I have in my system when the time is up. I have been trying to write this program and I can't understand why my code doesn't run the way I want it to. Could someone please point out if there's a mistake in it?
What's wrong with your code specifically is this condition
(t.tm_sec) != T+(t.tm_sec)
The problem is that t's value was set when you did (t.tm_sec) != T+(t.tm_sec). Once that is set, the same value of t will be used. I think you assumed that t will be recomputed every time in the while statement. To recompute t every time you can do:
import time
import os
T = int(input("Enter desired time for the timer - "))
snap_time = time.localtime(time.time())
def Timer():
t = time.localtime(time.time())
while t.tm_sec < (T + snap_time.tm_sec):
t = time.localtime(time.time())
os.system("start C:/Users/Public/Music/Sample Music")
Timer()
t.tm_sec is fixed, so unless T is 0 the condition will never be False, so the else block will never be executed. On top of that there is return in the loop, which mean it will run only one iteration and exit the function. Try
T = int(input("Enter desired time for the timer - ")) + time.time()
def Timer():
while time.time() < T:
pass
else:
os.system("start C:/Users/Public/Music/Sample Music")
You can also remove the else
def Timer():
while time.time() < T:
pass
os.system("start C:/Users/Public/Music/Sample Music")
I don't think your example makes proper use of while (and return). How about a much simpler version:
import time
import os
T = int(input("Enter desired time for the timer - "))
def Timer():
time.sleep(T)
os.system("start C:/Users/Public/Music/Sample Music")
Timer()
import time
from threading import Timer
from random import randint
print("Every wrong answer is a 3s delay; you have 30s")
end = False
def lose():
print(end)
print("Time up!")
time.sleep(1)
print("Score is",pts,", with",wrong,"wrong answers.")
time.sleep(1)
input("enter to quit")
quit()
timer = Timer(10,lose)
timer.start()
pts = 0
wrong = 0
while end == False:
a = randint(5,50)
b = randint(5,50)
print(a,"+",b)
ans = input()
if ans.isnumeric():
ans = int(ans)
if ans == a+b:
print("correct")
pts = pts+1
else:
print("wrong,",a+b)
wrong = wrong+1
print("delay")
time.sleep(3)
print("delay end")
print("")
When the timer finishes, the loop overlaps the 'lose' function, and it messes up on the line like this:
Time up!
45 + 10
55
Score iscorrect
3
, with29 0+ wrong answers.37
enter to quitwrong,p
66
delay
How do I fix this issue?
Sorry if this question has already been answered, but I want to know.
Ideally, you should probably avoid using threads altogether, as mentioned in the comments.
However, if you are going to use threads, consider using a mutex to ensure that multiple threads are not trying to write to stdout at the same time.
For example:
# setup at the beginning:
from threading import Thread, Lock
mutex = Lock()
# surrounding each print statement:
mutex.acquire()
try:
print('text')
finally:
mutex.release()
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
so i am trying to figure this out for school. Im trying to print x out every minute and every ten min it will print on a new line. so far i cant get the "printing x" every min down. can someone please help.
this is my code
import time;
inTime = float(input("type in how many second"))
oldTime = time.time()-inTime
print (time.time())
def tenMin(oldTime):
newTime = time.time()
if ((newTime - oldTime)>= 25):
return True
else:
False
while (True):
if (tenMin==True):
print ("x")
newTime = time.time()
oldtime = time.time()
else:
oldTime = time.time()
continue
Your first problem is in the line
if (tenMin==True):
You compare a function reference to a boolean, clearly the answer would be False. You have to pass a parameter
if (tenMIn(oldTime)):
...
First you have some issues with you code:
else:
False - This is not true syntax in python.
If you want timer, why are you asking the user for input?
You have a logic problem:
inTime = float(input("type in how many second"))
oldTime = time.time()-inTime
time.time is float yes, but can a user really know what to print in UnixTime?
I'll suggest a simple solution it's not the very best but it works.
It will print "x" every 1 Min and after 10 Min it will print "\n" (new line)
import time
def main():
#both timers are at the same start point
startTimerTen = time.time()
startTimerMin = startTimerTen
while True:
getCurrentTime = time.time()
if getCurrentTime - startTimerTen >= 600:
# restart both parameters
startTimerTen = getCurrentTime
startTimerMin = getCurrentTime
print "This in 10 min!\n"
if getCurrentTime - startTimerMin >= 60:
# restart only min parameter
startTimerMin = getCurrentTime
print "x"
#end of main
if __name__ == "__main__":
main()