Is there a way to make an infinite loop in python? [duplicate] - python

This question already has answers here:
Infinite for loop in Python like C++
(6 answers)
Closed 1 year ago.
I know you can just do this (technically):
import time
variable = 0
while variable < 99999999999999999999999999999999999999
variable = variable + 1
time.sleep(1)
But is there just a way to make an infinite loop without typing all those 9's?
(idk if this makes sense but I'm sure you get what I mean)

You can use
import time
while True:
time.sleep(1)
Because you want an infinite loop with a one-second delay between iterations.

Related

How to execute a function at the nth second of every minute? [duplicate]

This question already has answers here:
Repeat python function at every system clock minute
(3 answers)
Closed 8 months ago.
time.sleep(60) is oblivious to the time.
So how to get python to execute a function at say the 50-second mark of every minute?
EDIT: I mean I want my function to be executed at:
-20:00:50
-20:01:50
-20:02:50
and so on...
Just put it in a loop:
while True:
my_function()
time.sleep(50)

What the difference between while and for loop [duplicate]

This question already has answers here:
When to use "while" or "for" in Python
(10 answers)
Closed 1 year ago.
There is a really differ between while and for in python? Or can I use anyone I want?
And my second question :-
persons['alic', 'sam', 'rain']
first_name = 'mike'
for first_name in persons :
#when the program runs for the first time
#what is the value of first_name?
#is it 'mike',or its value is Null??
print(first_name)
And thanks.
In general, a for in loop is useful for iteration over a known set and the number of iterations is predetermined. A while loop is generally used when the exit condition is a change of state, not a predetermined length.
Common use cases for while loops include:
Running the main loop of a program until a keyboard interrupt:
try:
while True:
break
except KeyboardInterrupt:
print("Press Ctrl-C to terminate while statement")
pass
Finding the last node in a list of references:
while(node is not None):
node = node.next()
This question is answered well in this stackoverflow
Pulling out the information:
while loop - used for looping until a condition is satisfied and when it is unsure how many times the code should be in loop
for loop - used for looping until a condition is satisfied but it is used when you know how many times the code needs to be in loop
do while loop - executes the content of the loop once before checking the condition of the while.

How to restart a code included into a function? [duplicate]

This question already has answers here:
How to test multiple variables for equality against a single value?
(31 answers)
Closed 1 year ago.
I would like to restart my python code from the beginning after a given "yes" input from the user, but I can't understand what I'm doing wrong here:
if input("Other questions? ") == 'yes' or 'yeah':
main()
else:
pass
my code is included within the function main().
Thanks for the help!!
You would probably do it with a while loop around the whole thing you want repeated, and as Loic RW said in the comments, just break out of the while loop:
while True:
# do whatever you want
# at the end, you ask your question:
if input("Other questions? ") in ['yes','yeah']:
# this will loop around again
pass
else:
# this will break out of the while loop
break

Im trying to use time in python as a trigger for a comand [duplicate]

This question already has answers here:
How to use at command to set python script execute at specified time
(4 answers)
Closed 3 years ago.
im trying to make a command happen when its a certain time but its not working someone please help thanks,
here is my code
from datetime import datetime
import time
while True:
sam = (datetime.now().time())
ssam = str(sam)
if ssam == ('23:12:24.092411'):
print("hui")
else:
pass
The odds of calling datetime.now().time() at a precise microsecond are slim. One-second accuracy would probably be sufficient, and if so, you don't want to call datetime.now().time() as fast as you can; even a one-second delay in the loop saves thousands of unnecessary calls.
import datetime
import time
at_time = datetime.time(23, 14, 12)
while datetime.datetime.now().time() < at_time:
time.sleep(1)
print("hui")

Python loop that always executes at least once? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
do-while loop in Python?
How do I write a loop in Python that will always be executed at least once, with the test being performed after the first iteration? (different to the 20 or so python examples of for and while loops I found through google and python docs)
while True:
#loop body
if (!condition): break
You could try:
def loop_body():
# implicitly return None, which is false-ish, at the end
while loop_body() or condition: pass
But realistically I think I would do it the other way. In practice, you don't really need it that often anyway. (Even less often than you think; try to refactor in other ways.)

Categories

Resources