While Loops and The Break Statement in Python (Python Tutorial #6)

///While Loops and The Break Statement in Python (Python Tutorial #6)

While Loops and The Break Statement in Python (Python Tutorial #6)

FavoriteLoadingAdd to favorites

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

By |2020-08-31T09:12:19+00:00August 31st, 2020|Python Video Tutorials|25 Comments

25 Comments

  1. indoorbear August 31, 2020 at 9:12 am - Reply

    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

  2. AK August 31, 2020 at 9:12 am - Reply

    why is there [i] after given list at 4:24

  3. Jovian y August 31, 2020 at 9:12 am - Reply

    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)

  4. Javi Reyes August 31, 2020 at 9:12 am - Reply

    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)

  5. ADHILA MOIDEEN August 31, 2020 at 9:12 am - Reply

    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)

  6. Anar Rzayev August 31, 2020 at 9:12 am - Reply

    Liked Obama's slogan. I remember him telling Google CEO that we can bubble sort if we wanna sort given array.

  7. Luffy D knight August 31, 2020 at 9:12 am - Reply

    ans is -5

  8. Luffy D knight August 31, 2020 at 9:12 am - Reply

    oi guys use i< len(given_list)

  9. gamcreatsam August 31, 2020 at 9:12 am - Reply

    lol
    you are too awesome
    i like your content and the way how you teach difficult things so easily

  10. Rea x August 31, 2020 at 9:12 am - Reply

    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)

  11. saurabh pareek August 31, 2020 at 9:12 am - Reply

    total5 = 0

    i = 9

    while True:

    total5 += given_list3[i]

    i -= 1

    if given_list3[i] >= 0:

    break

    print(total5)

  12. Brisa Queque August 31, 2020 at 9:12 am - Reply

    I don't understand why there is variable INDEX. HELP PLEASE

  13. saurabh pareek August 31, 2020 at 9:12 am - Reply

    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)

  14. Im Not that Guy August 31, 2020 at 9:12 am - Reply

    Anyone else having their code not work and getting a hour glass in the tab

  15. Shivam Mehra August 31, 2020 at 9:12 am - Reply

    >>> 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?

  16. MR. PANDA August 31, 2020 at 9:12 am - Reply

    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

  17. Elizabeth Kirchner August 31, 2020 at 9:12 am - Reply

    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)

  18. Ayesha Noor August 31, 2020 at 9:12 am - Reply

    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)

  19. RICARDO SEUNG JAE LEE August 31, 2020 at 9:12 am - Reply

    you are awesome. thank you.

  20. Ly Soknorakodom August 31, 2020 at 9:12 am - Reply

    How j become 10 so fast

  21. Kai Kisseadoo August 31, 2020 at 9:12 am - Reply

    (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)

  22. Mithun Kannan August 31, 2020 at 9:12 am - Reply

    list = [7, 5, 4, 4, 3, 1, -2, -3, -5, -7]

    total = 0

    for i in list:

    if i < 0:

    total += i

    print (total)

  23. Biswadeep Choudhury August 31, 2020 at 9:12 am - Reply

    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)

  24. Junaid August 31, 2020 at 9:12 am - Reply

    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)

  25. Maurizio Moretti August 31, 2020 at 9:12 am - Reply

    # 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)

Leave A Comment

*