How can I create an infinite "For" loop capable of storing values ​​without using a "while"?

I am learning Python (using 3.6.2) and in their last class, they asked me to do something where I need to do an infinite loop . For some reason, the teacher does not want us to use for the whole practice. Here it gets complicated ... forwhile

So, I was looking for a way to do this. But it’s also difficult because the teacher doesn’t want us to use commands that we did not see in the classroom . Therefore, I can not use the functions .append, sys, well, I can not even use the break. I have to find a way to do with the "simple" commands .

I thought I could do it like this;

x=1
    for i in range(x):
    do_something()
    x += 1

However, this did not seem to work. I think that since Python no longer reads the value for the range?

I could not find a way, but after several hours of reflection, I found a small solution that I could use:

def ex():
    print("Welcome")
    for i in range(1):
        math = int(input("Please insert grades obtained at Math, or insert 666 to exit" ))
        if(math > 0 and math < 60):
            print("Sorry. You failed the test")
            return ex():
        elif(math >= 60 and math <= 100):
            print("Congratulations. You passed the test")
            return ex():
        elif(math == 666):
            return exit()
        else:
            print("ERROR: Please insert a valid number")
            return ex():

def exit():
     pass

As you can see, what makes it “infinite” is that it returns to the function once and again until you tell the program to “exit” by entering “666”. I would also like to have a more correct way to exit the function.

I am still wondering if there is a better way to make my loop endless until the user calls it to stop. However, one way or another I got this exercise. The problem arose when I started with the second one , which more or less looks like this:

, , . , , . (), "" .

, , , . , .

. , , while:( , - , ...

, 3 :

  • "" ?
  • ""?
  • ?

, , . , , - :)

EDIT: , itertools, , , . , for ?

+4
4

itertools, while, ;

. , python for.

, , . , , .

; , (.. foreach), , , ?


; , itertools, , ;

from itertools import count
for i in count():
    pass  # this will loop for longer than you will live.

, , .

+1

- :

for i in iter(int, 1):
    print("Infinite for loop executing")

while .

+5

"" , , Python . ...

, , for. , .

, return, break.

def get_data(prompt):
    lst = [None]
    for i in lst:
        s = input(prompt)
        if not s:
            return lst[1:]
        lst += [int(s)]
        print(lst)

print('Enter data, one number at a time. Enter an empty line at the end of the data')
lst = get_data('Number: ')
print('Data:', lst)

Enter data, one number at a time. Enter an empty line at the end of the data
Number: 3
[None, 3]
Number: 1
[None, 3, 1]
Number: 4
[None, 3, 1, 4]
Number: 1
[None, 3, 1, 4, 1]
Number: 5
[None, 3, 1, 4, 1, 5]
Number: 9
[None, 3, 1, 4, 1, 5, 9]
Number: 
Data: [3, 1, 4, 1, 5, 9]
+5

Python 3.x , :

class loop_iter(object):
    def __iter__(self):
        return self
    def __next__(self):
        # for python2.x rename this method to `next`

        # Return the next item from the container.
        # If there are no further items, raise the StopIteration exception.
        # Because this is infinite loop - StopIteration is not raised
        return None

class infinite_loop(object):
    def __iter__(self):
        # This method is called when an iterator is required for a container. 
        # This method should return a new iterator object that can iterate over all the objects in the container.
        # For mappings, it should iterate over the keys of the container, and should also be made available as the method keys().

        # Iterator objects also need to implement this method;
        # they are required to return themselves.
        # For more information on iterator objects, see Iterator Types.
        return loop_iter()

x = []
# thx PM 2Ring for text
print('Enter data, one number at a time. Enter an empty line at the end of the data')  

for _ in infinite_loop():
    line = input('Number: ')

    if line:
       x += [int(line)]
       continue

    print('Average:', sum(x)/len(x))  # average
    exit()
0

Source: https://habr.com/ru/post/1687532/


All Articles