Python I have this code import subprocess import datetime from time import sleep
ID: 3749442 • Letter: P
Question
Python
I have this code
import subprocess
import datetime
from time import sleep
i = 0
while i<100:
try:
subprocess.check_output(['ping -l 1000 www.google.com c- 100'])
except:
print("Ping google.com:", datetime.datetime.now())
def pumpcheck(timer_in_minutes):
time_clicked = datetime.datetime.now()
now = time_clicked
timeDiff = datetime.timedelta(minutes=int(timer_in_minutes=.001))
while(time_clicked + timeDiff > now):
print ('pumpOn')
now = datetime.datetime.now()
sleep(2)
print ('pumpOff')
i += 1
I need to loop 120 times and stop Can you help?
Please type your answer
Explanation / Answer
I got your question, please provide indentation next time onwards, you want the second loop (while loop, in the pumpcheck) to be executed for only 120 times. You can use a counter, that will be implemented at every iteration, and breaks the loop at 120th iteration.
I have modified your code accordingly, I initialized a counter as variable j , which would loop after each loop, and breaks the loop if it is equal or greater than 120. This will let the loop runs exactly 120 times.
I bother if you want to use i as counter and want to break at, when i == 120, just replace it with the if condition and remove the j initialization.
Here is the code:
import subprocess
import datetime
from time import sleep
i = 0
while i<100:
try:
subprocess.check_output(['ping -l 1000 www.google.com c- 100'])
except:
print("Ping google.com:", datetime.datetime.now())
def pumpcheck(timer_in_minutes):
time_clicked = datetime.datetime.now()
now = time_clicked
timeDiff = datetime.timedelta(minutes=int(timer_in_minutes=.001))
while(time_clicked + timeDiff > now):
j=0
print('pumpOn')
if j >= 120:
break
now = datetime.datetime.now()
sleep(2)
print ('pumpOff')
i += 1
j += 1
Here, above is the code.
Hope this will help you. If you have any further query, feel free to ask.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.