How to save values ​​from the for loop? Python 3

Yesterday I posted a question where I was looking for a way to make an endless loop without using at all (because my teacher therefore wants, and also, we cannot use any commands that we did not see in the class). This was tricky because there was apparently not a very viable option that didn't use any other features like either .append, etc. for whilewhileitertools

You can see this question here.
Also, thank you very much for the feedback you guys brought me! :)


But I managed to talk with my teacher, and we got permission to use, itertools or just a range, large enough (and not actually endless).


I have decided a few exercises already, but now I have the following instructions:

(Think of classes )
• Ask the user for a number (through the inputs) and keep asking until the user tells you to stop.
• Then calculate the average of all the numbers entered.

(This is actually a bit more complicated, but I cut it down and I think I can handle the rest)

As I said, I have to use a loop for, but I cannot use whilesat all.

while, - :

def grades():
    totalg = 0
    countg = 0
    keepAdding = "y"
    while(keepAdding == "y"):
        qualif = int(input("Insert grades obtained, in scale from 0 to 100 "))
        totalg = totalg + qualif
        countg = countg + 1
        keepAdding = str(input("Do you wish to keep adding data? (y/n) "))
    print("The average of your grades is", totalg/countg)

- , for? , .


, "" , break.

! !:)

+2
3

- - two-arg iter; , - , - , , .

, , - :

 for _ in iter(bool, True):

bool False, True , .

, , , -, 'q' quit ( ), :

for inp in iter(lambda: input("Insert grades obtained, in scale from 0 to 100 (type 'q' to quit)"), 'q'):
    val = int(inp)
    ... rest of loop ...

, (-arg iter ), while True: , test-and- break test-and- return ( ). , :

 try:
     for ...:
         if test_for_end:
             raise StopIteration
 except StopIteration:
     pass
 # You're outside the loop

. , , , while True: test-and- break/return. , , , .

+4

, "no- break" "" , , return:

grades = [0]
for j in grades:
    t = int(raw_input('grade:'))
    ans = raw_input('more? [y/n]:').lower()
    if(ans == 'y'):
        grades.append(t)
    else:
        grades[0] = t

print(sum(grades)*1.0/len(grades))  

, - , .

, ShadowRanger - .

+3

Is it possible to save all the data received in the array? I think python arrays work like Linked Lists, so you won't have any overflow problems, at least this will be the starting point

-1
source

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


All Articles