How to make infinite loop by using FOR LOOP ? Solved. | Sololearn: Learn to code for FREE!

+4

How to make infinite loop by using FOR LOOP ? Solved.

I can do it by WHILE LOOP only.

2/9/2020 3:21:34 PM

Cp Chaudhary

36 Answers

New Answer

+21

lst = [1, 2, 3] for i in lst: print("hello") lst.append(i) This will eventually consume all available memory as the list size is growing. You can also do something like this from itertools import cycle for x in cycle(range(1)): print(x)

+12

You can not directly do it with a for loop, because a for loop always loops over an iterable. The iterable, however, can be built in a way that it dishes out values forever. (EDIT: Diegos example shows one example of that sort of iterable.) Please look at this question from a month ago which is basically what you're asking for. https://www.sololearn.com/discuss/2132309/?ref=app

+11

import itertools for i in itertools.count(): print(i) break # Remove this to create an infinite loop

+11

Hello @Jacob A friendly reminder that the behavior you've exhibited on this thread thus far is inappropriate. To cite our Community Guidelines: "SoloLearn's policies are based on two key principles: - Posts by SoloLearn community members should be programming-related and aimed at helping individual learners and community members improve their programming skills by creating a respectful resource base. Posts may also focus on improving aspects of the platform and providing the community with improved content, whenever applicable. - Personal attacks or disparagement of other users or their content is not tolerated. Every community member should be respectful and considerate, and assume that other people are also trying to make SoloLearn a great development resource. This means respecting opposing or differing opinions, beliefs, and conclusions; making an effort to listen to and understand others with whom you may disagree; and encouraging others to also be welcoming and respectful." https://www.sololearn.com/Content-Creation-Guidelines/ Please note the number of occurrences of the word "respectful" in the above excerpt. I found the following replies to be noncompliant: - "i dont care what u think," - "~ swim ~ yea, STOP POSTING ABOUT UNRELATED TOPICS PLEASE AND THANK YOU" - "~ swim ~ i didnt know about caps lock, but i do know i wont get banned for this" Please remove your posts in a timely fashion or I will remove them for you. If you continue to behave in a noncompliant manner, moderators do have the power to ban you -- contrary to what you've expressed. Please take heed. Warmest regards, -- Janning SoloLearn Gold Moderator

+9

Mùķę§h⅔ yes I didn't read python that's why I deleted my answer as soon as I saw python.

+9

Hello Everyone, As ~ swim ~ said: "Please learn to curb the urge to post unnecessarily. Read the question properly before posting." Irrelevant posts substantially lower the quality of information available in our forum. Therefore, as a moderator, I can clean up this thread by removing any replies I determine to be off-topic. If you have any doubts about this, please review our Community Guidelines at: https://www.sololearn.com/Content-Creation-Guidelines/ As it states: "If your content/post is not specifically on-topic, it can be deleted." In the meantime, please consider providing helpful/useful insights in thread(s) which may actually be relevant to what you wanted to share. You can practice using the search feature to find such threads. Happy coding! Best, -- Janning⭐ SoloLearn Gold Moderator

+7

Jacob Burgess It's an attempt to keep threads clean. Try this sort of nonsense on StackOverflow, you will be banned almost immediately. Do you want sololearn to ultimately go that way? If not better don't ask me to stop, you shouldn't be doing it yourself and keep your head cool (those caps lock). Enjoy your day :)

+5

VRDANG, this syntax is in c++ not in python..

+4

Ok I got it

+4

def lettersforever(): n = 0 while True: yield chr(n+97) n = (n+1)%26 for letter in lettersforever(): print(letter)

+4

ClassHacker Read the question before posting. The question is for python. There is no point in posting unnecessarily in someone's else thread.

+4

Same goes for 😈😈😈😈😈😈😈: This is about for loops, which have no conditions. Please read questions properly, and ideally also the formerly given answers!

+4

It is not possible to make an infinite loop with for loop because it will iterates based on the number of elements in the sequence. Syntax for For loop: for variable in sequence Infinite loop is possible with while loop.

+4

Thanks for everyone but really I not need any more answer.

+4

kunal chandra Please read the question properly before posting. The question is for python language and your answer is not for python.

+3

+3

Cp Chaudhary, you can edit your question and write [solved] in front of the title. It might help... somewhat. 🙄 Also you can click the star in the lower left of your question, then you won't get any more notifications.

+3

for( i=0; true; i++) { ... }

+2

Jacob Burgess, 'I don't know python'. So why not stop right there? What do you hope to achieve adding information that has no relation to the question?

+2

Harikrishna Haridevan , w¡1d3r💤 Do you guys even read the question properly? w¡1d3r💤 The question is for python not C/C++ or other language. Harikrishna Haridevan From what angle is your answer relevent to the question asked? Please learn to curb the urge to post unnecessarily. Read the question properly before posting.