

How to use while loops and the break statement in Python.
This entire series in a playlist:
Keep in touch on Facebook:
Download the sample file:
Subscribe to my newsletter:
Support me on Patreon:
source
How to use while loops and the break statement in Python.
This entire series in a playlist:
Keep in touch on Facebook:
Download the sample file:
Subscribe to my newsletter:
Support me on Patreon:
source
I don’t know why but I think CSDojo is the only person I don’t mind giving me homework, even if it’s like very hard I will still don’t mind XD
why is there [i] after given list at 4:24
my solution:
# for loop
list_task = [7, 5, 4, 3, 2, 1, -1, -2, -3, -5, -6]
total_sum = 0
for num in list_task:
if num < 0:
total_sum += num
print(total_sum)
# while loop
total_sum2 = 0
index = -1
while True:
total_sum2 += list_task[index]
index -= 1
if list_task[index] > 0:
break
print(total_sum2)
somehow got it like this didn't see any other like it a little confused.
given_list3 = given_list = [7, 5, 4, 4, 3, 1, -2, -3, -5, -7]
total6 = 0
i = -1
while True:
total6 += given_list3[i]
i += -1
if given_list3[i] >= 0:
break
print(total6)
b=[7, 5, 4, 4, 3, 1, -2, -3, -5, -7]
while i < len(b):
if b[i]<0:
t+=b[i]
i+=1
i tried for hours trying to solve but it just wouldnt work
then i came to the comment section found out that i had put extra spaces so the correct ansewr is :
b=[7, 5, 4, 4, 3, 1, -2, -3, -5, -7]
while i < len(b):
if b[i]<0:
t+=b[i]
i+=1
its a very small error b(sorry for different variables used)
Liked Obama's slogan. I remember him telling Google CEO that we can bubble sort if we wanna sort given array.
ans is -5
oi guys use i< len(given_list)
lol
you are too awesome
i like your content and the way how you teach difficult things so easily
i solved the last question using both for and while loop
for loop:
list = [7, 5, 4, 4, 3, 1, -2, -3, -5, -7]
total = 0
for element in list:
if element < 0:
total += element
print(total)
while loop:
list = [7, 5, 4, 4, 3, 1, -2, -3, -5, -7]
i = len(list)-1
total = 0
while True:
total += list[i]
i -= 1
if list[i] > 0:
break
print(total)
total5 = 0
i = 9
while True:
total5 += given_list3[i]
i -= 1
if given_list3[i] >= 0:
break
print(total5)
I don't understand why there is variable INDEX. HELP PLEASE
given_list3 = [7, 5, 4, 4, 3, 1, -2, -3, -5, -7]
total2 = 0
for element in given_list3:
if element <= 0:
total2 += element
print(total2)
Anyone else having their code not work and getting a hour glass in the tab
>>> list = [5,4,4,3,1,-2,-3,-5]
>>> total = 0
>>> for element in list :
… if element < 0 : break
… total = total + element
… print(total)
File "<stdin>", line 4
print(total)
^
SyntaxError: invalid syntax
what is the error?
given_list3 = [7, 5, 4, 4, 3, 1, -2, -3, -5, -7]
total6 = 0
i = 9
while True:
total6 += given_list3[i]
i -= 1
if given_list3[i] >= 0:
break
print(total6)
i did reverse
given_list3 = [7, 5, 4, 4, 3, 1, -2, -3, -5, -7]
total6 = 0
index = 0
while index < len(given_list3):
if given_list3[index] < 0:
total6 += given_list3[index]
index += 1
print(total6)
hey please someone help me this code is giving me 10 answer. Tell me why ? Am i making a mistake?
list = [5, 4, 4, 3, 1, -2, -3, -5]
t = 0
i = 0
while list[i] > 0:
t += list[i]
list[i] += 1
print(t)
you are awesome. thank you.
How j become 10 so fast
(Day 1 of learning to code) I don't know if i failed my way to the right answer but this is what i got:
given_list3 = [7, 5, 4, 4, 3, 1, -2, -3, -5, -7]
total6 = 0
u = 6
while u < len(given_list3) and given_list3[u] < 0:
total6 += given_list3[u]
u += 1
print(total6)
list = [7, 5, 4, 4, 3, 1, -2, -3, -5, -7]
total = 0
for i in list:
if i < 0:
total += i
print (total)
So as the given list is sorted here is what we can do in order to minimize number of iterations.
# PRINT THE SUM OF ALL THE NEGATIVE NUMBERS IN A SORTED LIST
def sum_negative(arr):
i = len(arr) – 1
total = 0
while(arr[i] < 0):
total += arr[i]
i -= 1
return total
a = [7, 5, 4, 4, 3, 1, -2, -3, -5, -7]
sum1 = sum_negative(a)
print(sum1)
Can anyone pls tell if I am correct or not
n = [7, 5, 4, 4, 3, 1, -2, -3, -5, -7]
sum = 0
for i in range(0, 10):
if n[i] < 0:
sum = sum + n[i]
print(sum)
# sum all negative numbers in descending sorted given list
# while loop implementation
given_list3 = [7, 5, 4, 4, 3, 1, -2, -3, -5, -7]
total5 = 0
i = len(given_list3) – 1
while True:
total5 += given_list3[i]
i -= 1
if given_list3[i] >= 0 or i < 0:
break
print(total5)
# for loop implementation
total5 = 0
for i in range(len(given_list3)-1,-1,-1):
if given_list3[i] >= 0:
break
total5 += given_list3[i]
print(total5)