How to get data out of a while loop?
Question:
I want to make a summation of the collected data from a while loop because I want to calculate Youtube video duration to divide them into days. I want to make something which collects the final answer from each loop and makes a summation process of them. How can I do that?
My code is:
while True:
def youtube_calculater(x):
return x * 60
a = youtube_calculater(float(input("enter minutes: ")))
def sum_operation(y):
return a + y
b = sum_operation(float(input("enter seconds: ")))
def final_operation(n):
return n / 60
s = final_operation(b)
print(s)
Answers:
You could store every calculated s
inside a list (I named it s_list
in the example) and then use sum()
to calculate the sum of all the entries.
s_list = []
while True:
def youtube_calculater(x):
return x * 60
a = youtube_calculater(float(input("enter minutes: ")))
def sum_operation(y):
return a + y
b = sum_operation(float(input("enter seconds: ")))
def final_operation(n):
return n / 60
s = final_operation(b)
s_list.append(s)
print(s)
print(sum(s_list))
If you dont care about every entry in the list. You can just create a variable sum_s
the same way and always add s
to the sum using sum_s += s
.
I want to make a summation of the collected data from a while loop because I want to calculate Youtube video duration to divide them into days. I want to make something which collects the final answer from each loop and makes a summation process of them. How can I do that?
My code is:
while True:
def youtube_calculater(x):
return x * 60
a = youtube_calculater(float(input("enter minutes: ")))
def sum_operation(y):
return a + y
b = sum_operation(float(input("enter seconds: ")))
def final_operation(n):
return n / 60
s = final_operation(b)
print(s)
You could store every calculated s
inside a list (I named it s_list
in the example) and then use sum()
to calculate the sum of all the entries.
s_list = []
while True:
def youtube_calculater(x):
return x * 60
a = youtube_calculater(float(input("enter minutes: ")))
def sum_operation(y):
return a + y
b = sum_operation(float(input("enter seconds: ")))
def final_operation(n):
return n / 60
s = final_operation(b)
s_list.append(s)
print(s)
print(sum(s_list))
If you dont care about every entry in the list. You can just create a variable sum_s
the same way and always add s
to the sum using sum_s += s
.