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

  Рет қаралды 772,663

CS Dojo

CS Dojo

Күн бұрын

Пікірлер: 1 700
@sata2396
@sata2396 3 жыл бұрын
For anyone watching in 2021, if you press Shift+Enter you will run the code. Not sure if master YK has mentioned that. Also, if you don't understand the why of 'i += 1' like me at first, you're not adding a 1 to the element on the list (i.e. 4+1), you're telling the code to go 1 position up on the list (in this list 5 is in spot #0, 4 in spot #2 and so on) Hope this helps someone!
@cicada3312
@cicada3312 2 жыл бұрын
this helps so much,
@LemonBoi591
@LemonBoi591 2 жыл бұрын
yeah u can use shift+enter to run the code but u cannot automatically add a new cell to the file so if u want to do it alongside with adding a new cell its better to use the run button.
@joeroganpodfantasy42
@joeroganpodfantasy42 2 жыл бұрын
@@LemonBoi591 It adds a new cell for me there's three shortcuts Alt+Enter Ctrl+Enter this is like the one you describe doesnt add a new cell Shift+Enter
@joeroganpodfantasy42
@joeroganpodfantasy42 2 жыл бұрын
the first line that has the total is adding the number the second line j+=i is just doing the looping thru the positions that's how I like to think about it. I remember in school learning C or C++ don't know if it was the professor's idea or mine to just go ahead and do the looping by hand everytime it was good practice cause by doing it by hand you get it ingrained in your head whats going on and you can actually visualize the process. You can do it by hand in these simple problems then when it comes time for hard problems you can just visualize it and not go thru the motions and appreciate the value of it,
@LemonBoi591
@LemonBoi591 2 жыл бұрын
@@joeroganpodfantasy42 Oh ok which one adds a new cell tho shift enter it alt enter
@bluebellhs
@bluebellhs 5 жыл бұрын
I really love the fact that you give us a homework at the end of each session.
@mohammadzamani72
@mohammadzamani72 4 жыл бұрын
@ i'm asian too, so i get his point, we ALWAYS either taking or giving homework
@Username-lt7lt
@Username-lt7lt 4 жыл бұрын
Mohammad Zamani I'm caucasian and I love getting homework at the end of these videos
@beelz1042
@beelz1042 4 жыл бұрын
@@Username-lt7lt I'm African and I also love that he gives us homework , it's so entertaining and makes me feel proud of myself when I solve it
@mohammadzamani72
@mohammadzamani72 4 жыл бұрын
@@Username-lt7lt it's ok,but i JUST told that guy that Tanbir's comment is ACUURATE, wh? cause he apperently didn't get the Tanbir's joke! ok i know a lot of guys like homeworks, just like a lot of guys love chickens, but when a person of color says i like chicken it's funny, it's stereotypically funny. it's a JOK.
@oxane2722
@oxane2722 3 жыл бұрын
Hehe, such homeworks where there'd be no problem if don't do😁
@AWGERSS
@AWGERSS 4 жыл бұрын
I love how active this comment section is. Even though it was made 2 years ago more and more people are still saying stuff in the comments. Love it so much
@wlcus485
@wlcus485 10 ай бұрын
Yes
@filippians413
@filippians413 4 жыл бұрын
Clearly this tutorial is the breaking point for a lot of people learning Python. All I can say is the only way for it to make sense is to practice it as much as possible. I'm struggling too, don't worry lol
@Assassin-wt8ij
@Assassin-wt8ij 4 жыл бұрын
for the following program. How did we get 10 as the answer. total2 = 0 j = 1 while j < 5: total2 += j j += 1 print(total2) please answer it someone. How did we get 10 out of it?
@xnonqme3716
@xnonqme3716 3 жыл бұрын
Keep in mind that this is a 'loop' meaning it is repeating/going through each number individually.
@codyjames9209
@codyjames9209 3 жыл бұрын
@@Assassin-wt8ij Holy shit i feel you. Right when i saw the answer i was confused haha.
@unstoppableluckq9985
@unstoppableluckq9985 3 жыл бұрын
Any encouragement being a programmer is my dream and I’m fearing this may as you said be my breaking point. I’ll keep on trying for now!
@mariannedaplin7559
@mariannedaplin7559 3 жыл бұрын
@@Assassin-wt8ij it will perform total2 += j so long as j < 5. To break it down, When j = 1, it will perform 0 + 1, which results into a new 'total2' of 1 When j = 2, it will perform 1 + 2, so now total2 = 3 When j = 3, it will perform 3 + 3, so now total2 = 6 When j = 4, it will perform 6 + 4, so now total2 = 10 When j = 5, the condition is no longer met so the loop ends, giving a final answer of 10
@jamesdang236
@jamesdang236 5 жыл бұрын
total6 = 0 i = 0 while i < len(given_list3): if given_list3[i] < 0: total6 += given_list3[i] i += 1 print(total6)
@nellowz5451
@nellowz5451 4 жыл бұрын
All this time I was writing while i < given_list3: Thanks man
@にゃ-c2f
@にゃ-c2f 4 жыл бұрын
I was able to find the answer with the "for loop" statement much easier than the "while loop" statement. It took me an hour before I finally figured out the solution using the "while loop" statement, on the other hand it took me 2 mins to figure it out using the "for loop." HHAHAHAHAHHAHAHAHAH
@MrBenIPresume
@MrBenIPresume 6 жыл бұрын
given_list = [ ] total = 0 for element in given_list: if element < 0: total += element print(total) This actually works regardless of the order of the indexed numbers, so they don't have to be ascending or descending.
@BLAQ_HINTS
@BLAQ_HINTS 5 ай бұрын
although it will work but It will not end since you didn't give it an ending condition. there are infinity negative numbers out there.
@sunbags
@sunbags 6 жыл бұрын
This is the first youtube coding tutorial series I've that actually teaches. Very happy with these. Will be recommending to others who wish to learn Python
@ovyeez3063
@ovyeez3063 4 жыл бұрын
here's something inspirational I am just a 10 year old kid but i'm carrying on with coding even when I get confused. What I do is go through one tutorial every half an hour and by the end of the half an hour i've learnt something new, you don't need to rush just slow down and carry on coding.
@kingdomovies
@kingdomovies 3 жыл бұрын
Why are you so rational at 10 years old
@krle8109
@krle8109 3 жыл бұрын
@@kingdomovies lol ikr I wish I was the same when I was 10 or 15 or even now,,,,
@xLunarLight
@xLunarLight 6 жыл бұрын
given_list = [7, 5, 4, 4, 3, 1, -2, -3, -5, -7] total = 0 i = 0 while i < len(given_list): if given_list[i]
@jundenverllenares5552
@jundenverllenares5552 6 жыл бұрын
list = [7, 5, 4, 4, 3, 1, -2, -3, -5 , -7] i = 0 total=0 # for counting number of elements i=0 to i=9 while list[i] > -7: i+=1 # getting the total backwards, from -7 to -2 while list[i] < 0: total += list[i] i -= 1 print(total) >>>> -17
@jundenverllenares5552
@jundenverllenares5552 6 жыл бұрын
using "len" is cheating:D
@wesd3370
@wesd3370 6 жыл бұрын
Thanks posting for that... I had the same thing except that my line for i += 1 was within the if statement... so it was stopping on the first variable because the number wasn't < 0.
@dinocelikovic5847
@dinocelikovic5847 6 жыл бұрын
I did it with a 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) -17
@yuzetian8512
@yuzetian8512 6 жыл бұрын
Dino Čeliković j
@supaluck5599
@supaluck5599 6 жыл бұрын
given_list3 = [7,5,4,4,3,1,-2,-3,-5,-7] total6 = 0 i = 6 while i < len(given_list3): total6 += given_list3[i] i += 1 print(total6)
@rajkumarrathor8372
@rajkumarrathor8372 6 жыл бұрын
Sir I think the intrest of people in python is not so high according to views. Buy sir I am very interested in python. So don't stop this series of python tutorials. You are my favourite tutor for python . Love u sir
@matthewliu5161
@matthewliu5161 6 жыл бұрын
Sir, does that matter?
@kalazakan
@kalazakan 6 жыл бұрын
my cute mammoth because he's indian
@DayumAli
@DayumAli 6 жыл бұрын
sir
@glowish1993
@glowish1993 6 жыл бұрын
Sir, send booleans and variables
@m.n.uyeahiakhan6517
@m.n.uyeahiakhan6517 6 жыл бұрын
How to read user input until EOF? in C /C++ i use while( scanf("%d",&n) !=EOF ){ } but in Python ??????? please anyone help me
@guygreenleaf
@guygreenleaf 5 жыл бұрын
Wow. Im 24, about to turn 25. I've always had an affinity for computers and logic based things, but never properly utilized it due to laziness growing up. Now I work in a retail environment and although the people are cool, I can't stand the meaningless work. I was always scared/nervous to start down the CS/Software Engineering path but just through these simple tutorials alone you've opened my eyes to something I'm now spending every waking chance I get studying, and I'm even going back to school for Software Engineering. Thank you beyond words, I don't think I'll hate my life for much longer :)
@layr3592
@layr3592 5 жыл бұрын
it's not late yet. become a senior before turning 30 !
@Christopherjweaver
@Christopherjweaver 6 жыл бұрын
list = [2,-3,4,-5,-1,2,-3,-3,-3,7] total = 0 i = 0 while i < len(list): if list[i] < 0: print("less than 0", list[i]) total += list[i] i += 1 elif list[i] > 0: print("greater than 0", list[i]) i += 1 print(total)
@akim5702
@akim5702 5 жыл бұрын
I just had a huge brain fart after the elements lesson. I gotta stop and review. On another note, thank you for the lessons! I've moved 5 times this past year, so it made going to school impossible. You've made it possible for me to continue my education through these videos. Thank you, CSDojo!
@madbnm388
@madbnm388 3 жыл бұрын
list = [7, 5, 4, 4, 3, 1, -2, -3, -5, -7] Sum = 0 i = 0 while i < len(list): if list[i] < 0: Sum += list[i] i += 1 print(Sum)
@BorderlineHypeR
@BorderlineHypeR 4 жыл бұрын
Solution with comments: given_list3 = [7, 5, 4, 4, 3, 1, -2, -3, -5, -7] # Init list total6 = 0 # Init total m = 0 # Init counter while m < len(given_list3): # While the counter is less than list length (to avoid out of index errors) if given_list3[m] >= 0: # If the number at the counter index is >= 0 m += 1 # Just add to the counter, don't do any addition if given_list3[m] < 0: # However, if the number at index m is < 0 total6 += given_list3[m] # Add the number to the total m += 1 # Add 1 to the counter print(total6) # Print the total after the loop completes This is more long winded than JesusJFuncia's solution and I recommend you use theirs.
@rezeromylife6616
@rezeromylife6616 2 жыл бұрын
ty helped a lot
@dp31x2
@dp31x2 2 жыл бұрын
given_list3 = [7, 5, 4, 4, 4, 1, -2, -3, -5, -7] total6 = 0 i = -1 while True: total6 += given_list3[i] i = i - 1 if given_list3[i] >= 0: break print(total6)
@agusbrea1437
@agusbrea1437 2 жыл бұрын
given_list3 = [7, 5, 4, 4, 3, 1, -2, -3, -5, -7] total8= 0 i = 0 while i < len(given_list3): if given_list3[i] > 0: i += 1 else: total8 += given_list3[i] i += 1 print(total8)
@natnat5831
@natnat5831 3 жыл бұрын
given_list = [7, 5, 4, 4, 3, 1, -2, -3, -5, -7] total = 0 i = 0 while i < len(given_list): if given_list[i] > 0: i += 1 else: total += given_list[i] i += 1 print(total) it also works even if the order of positive and negative numbers are scrambled
@sashaalex404
@sashaalex404 3 жыл бұрын
this is so helpful !!! literally d best comment here : )
@waqasahmed9241
@waqasahmed9241 6 жыл бұрын
# Sum of negative numbers using while loop given_list3 = [7, 5, 4, 4, 3, 1, -2, -3, -5, -7] total3 = 0 i = 0 while i < len(given_list3): if given_list3[i] < 0: total3 += given_list3[i] i += 1 print (total3) # output = -17
@jodux6883
@jodux6883 5 жыл бұрын
I did the same
@constructaware350
@constructaware350 3 жыл бұрын
list_ = [7, 5, 4, 4, 3, 1, -2, -3, -5, -7] total = 0 i = 0 while i < len(list_): if list_[i] < 0: total += list_[i] i += 1 print(total) -17
@fennydobson1378
@fennydobson1378 3 жыл бұрын
There's no word to describe the amount of joy I experienced when I could code the homework and did not encounter any errors :D
@ashishsanjayekka7227
@ashishsanjayekka7227 6 жыл бұрын
list2 = [1,2,4,-3,-7,-9] total3 = 0 i = 0 while i < len(list2): if list2[i] < 0: total3 += list2[i] i = i+1 print(total3)
@wwemaniac371
@wwemaniac371 4 жыл бұрын
here's my take on the last porblem. see if it helps! happy coding!!! given_list3 = [7, 5, 4, 4, 3, 1, -2, -3, -5, -7] total7 = 0 i = 0 for element in given_list3: # this is to target the elements in the list if element < 0: # checking whether the int are neg or pos total7 += element # if neg, it will add the neg number to total7 i += 1 # for moving from index 0 to the last index print(total7) ans: -17
@zakozahbehni
@zakozahbehni 4 жыл бұрын
#Scanning through the list from the left using while loops :) given_list3 = [7,5,4,4,3,1,-2,-3,-5,-7] Total_neg = 0 j = 0 while j < len(given_list3) and given_list3[j] >= 0: j += 1 while j < len(given_list3) and given_list3[j]
@chrispeng7038
@chrispeng7038 6 жыл бұрын
given_list3 = [7,5,4,4,3,1,-2,-3,-5,-7] total6 = 0 for i in given_list3: if i < 0: total6 = total6 + i print (total6) The answer is -17. I think it's the easiest way.
@omarabubakr6621
@omarabubakr6621 6 жыл бұрын
yep you're right but try to do it with while also it would be better to do both :) good luck
@kristijanross977
@kristijanross977 6 жыл бұрын
It's easier if you put total6 += i instead of total6 = total6 + i
@TedPaul
@TedPaul 6 жыл бұрын
I guess one of the advantages is that he didn't have to define "i" because it's a FOR loop.
@da_ta
@da_ta 6 жыл бұрын
Easy one clever ! Thanks
@LuisMorales-yx8di
@LuisMorales-yx8di 5 жыл бұрын
what is ''i'' it isnt definite
@venxity6959
@venxity6959 2 жыл бұрын
given_list3 = [5, 4, 4, 3, 1, -2, -3, -5] total = 0 i = 0 while i < len(given_list3): if given_list3[i] < 0: total += given_list3[i] i += 1 print (total) -10 That's the code I came up with, and I'm super proud of it
@m.shakhawathussain6828
@m.shakhawathussain6828 6 жыл бұрын
given_list3 = [7,5,4,4,3,1,-2,-3,-5,-7] total = 0 for x in given_list3: if x
@nawabmakan7607
@nawabmakan7607 5 жыл бұрын
wrong the loop breaks at the first element
@thebedroomintellectual2460
@thebedroomintellectual2460 5 жыл бұрын
@@nawabmakan7607 wrong. The first element it looks at is
@mohankrishna2770
@mohankrishna2770 5 жыл бұрын
lst=[7,5,4,4,3,1,-2,-3,-5,-7] total=0 i=0 for i in lst: if i>=0: break total+=i print(total) can you tell me what's wrong with my code?
@thebedroomintellectual2460
@thebedroomintellectual2460 5 жыл бұрын
@@mohankrishna2770 You defined i=0. No need. You already have total=0.
@mohankrishna2770
@mohankrishna2770 5 жыл бұрын
@@thebedroomintellectual2460 Thank you
@uddalakbiswas2082
@uddalakbiswas2082 5 жыл бұрын
given_list2=[7,5,4,4,3,1,-2,-3,-5,-7] total3=0 k=0 while k < len(given_list2): while given_list2[k] < 0: total3 += given_list2[k] break k += 1 print(total3)
@Crockerfeller
@Crockerfeller 5 жыл бұрын
Another terrific tutorial! Thank you for your time and effort. My result for your "assignment" :) # Sum all negative numbers in given list given_list = [7, 5, 4, 4, 3, 1, -2, -3, -5, -7] total = 0 i = 0 while i < len(given_list3): if given_list[i] < 0: total += given_list[i] i += 1 print(total)
@paul-cl3el
@paul-cl3el 4 жыл бұрын
what is the meaning i < len(given_list3)
@Hansulf
@Hansulf 3 жыл бұрын
@@paul-cl3el len is for length which basicly outputs a number (like 4) for a vector like [1,2,3,-1,-2]... So while i is less than the lenght of the vector (the list of numbers), the loop continues. I made R while in University so I knew about this function, but I was trying so hard to get the result only using the things he teach us in the video. But no way...
@Hansulf
@Hansulf 3 жыл бұрын
Oh shit, len was in the video, Im just too bad as a students jajajajaja
@MotoCruiseTamil
@MotoCruiseTamil 4 жыл бұрын
given_list = [ 7, 5, 4, 4, 3, 1, -2, -3, -5, -7] total = 0 i = 0 while i < len(given_list): if given_list[i] >= 0: i += 1 else: total += given_list[i] i += 1 print(total) output: -17
@burlofreak
@burlofreak 5 жыл бұрын
I think I actually have an elegant solution: Start from the end of the list to save time; and only loop while your index meets your specifications: list3 = [7, 5, 4, 4, 3, 1, -2, -3, -5, -7] total6 = 0 i = len(list3)-1 while list3[i] < 0: total6 += list3[i] i -=1 print(total6)
@OldManTomJulio
@OldManTomJulio 4 жыл бұрын
This. You went about about it the more efficient way. Imagine if there a billion elements in the list and only the last number was negative. Everyone else's solution would still be running a "brute force" method while your program was done in a split second.
@sinyiheng3732
@sinyiheng3732 4 жыл бұрын
why have to -1 for the third line tho?
@jaqen7273
@jaqen7273 4 жыл бұрын
@@sinyiheng3732 because the list has number starting from 0. so if there are 8 elements in list to get to last element we have to go len(list)-1
@sinyiheng3732
@sinyiheng3732 4 жыл бұрын
@@jaqen7273 Oh ya omg I forgot :( Thank you so much for replying!!!
@PubertPenis
@PubertPenis 3 жыл бұрын
Yep this is exactly what I came up with too 👏🏻
@michaelsimons5502
@michaelsimons5502 6 жыл бұрын
given_list3 = [7, 5, 4, 4, 3, 1, -2, -3, -5, -7] total6=0 i=0 while i < len(given_list3): if given_list3[i] > 0: i += 1 else : total6 += given_list3[i] i += 1 print(total6) -17
@brisaqueque6820
@brisaqueque6820 4 жыл бұрын
I'm pretty sureprise for the views of this video. SO MANY HAS GIVEN UP GUYS KEEP GOING!!!!!!
@Traumbewusstsein
@Traumbewusstsein 6 жыл бұрын
thanks for the tutorials! my two versions of a solution: #using a while-loop, not going through the whole list b = [7, 5, 4, 4, 3, 1, -2, -3, -5, -7] sum2 = 0 j = len(b)-1 while b[j] < 0: sum2 += b[j] j -= 1 print('sum2 = ',sum2) #alternatively, using a for-loop which also doesn't go through the whole list sum3 = 0 for k in range (1, len(b)): if b[len(b)-k] >= 0: break sum3 += b[len(b)-k] print('sum3 = ',sum3)
@KhoiNguyen-jt9mr
@KhoiNguyen-jt9mr 3 жыл бұрын
can you explain more about the for-loop
@mmmdkrimov1218
@mmmdkrimov1218 2 жыл бұрын
Why we are using j=len (b) -1?
@dennissh7587
@dennissh7587 3 жыл бұрын
list = [7, 5, 4, 4, 3, 1, -2, -3, -5, -7] i = 6 total = 0 while i < len(list) and list[i] < 0: total += list[i] i += 1 print (total) -17
@daredonny22
@daredonny22 4 жыл бұрын
This is my code for this using for loop instead. Found it easier to be honest: given_list=[7,5,4,4,3,1,0,-5,-8,-9,-10] total=0 for i in range(len(given_list)): if given_list[i] < 0: total+=given_list[i] i+=1 print(total) While loop here: total2=0 k=0 while k
@tapfumadimairo4757
@tapfumadimairo4757 2 жыл бұрын
My code for the for loop was: given_list3 = [7, 5, 4, 4, 3, 1, -2, -3, -5, -7] total5 = 0 for element in given_list3: if element >= 0: break total5 += element print(total5) 0
@sadimohammadomi2556
@sadimohammadomi2556 5 жыл бұрын
given_list = [5,4,3,2,-2,-3,-4,-5] total=0 i=0 while i
@spacepinda
@spacepinda 5 жыл бұрын
Thank you this is what I was looking for!
@silkylonghairguy
@silkylonghairguy 4 жыл бұрын
Here's a solution: given_list = [7, 5, 4, 2, 1, 1, -1, -5, -9, -11] total = 0 i = len(given_list) - 1 while given_list[i] < 0: total += given_list[i] i -= 1 print(given_list) print(total)
@IndigoZZ
@IndigoZZ 4 жыл бұрын
This is the only solution working for me, thanks! Could you just explain why i = len(given_list) - 1?
@madhusudansharma5698
@madhusudansharma5698 4 жыл бұрын
We can reverse the list after that we can calculate
@arjunk5959
@arjunk5959 4 жыл бұрын
Reversing Index
@arjunk5959
@arjunk5959 4 жыл бұрын
Can you tell me why you reverse ? won't it work with normal indexing ?
@paitan
@paitan 2 жыл бұрын
​@@arjunk5959 Imma answer this even tho it's been a year since people watching now might have the same question as you: Probably because if you decided to do it like this: given_list = [7, 5, 4, 2, 1, 1, -1, -5, -9, -11] total = 0 i = 0 while given_list[i] < 0: total += given_list[i] i += 1 print(total) It would print 0, since it starts from the beginning of the list and the statement we gave tells it to only add to the total or progress to the next number if the number is less than 0. Since the numbers at the beginning of the list are positive numbers aka more than 0 and the statement only works for numbers less than zero, nothing would happen and it would just print total as it is (total = 0).
@김동민-j9p3y
@김동민-j9p3y 4 жыл бұрын
given_list3=[5,4,4,3,1,-2,-3,-5,-7] total6=0 i=0 while i
@philip3321
@philip3321 4 жыл бұрын
Here's my solution for the last exercise: list = [7, 5, 4, 4, 3, 1, -2, -3, -5, -7] total = 0 for x in list: if x > 0: continue total += x print(total) Output: -17
@oneinabillion654
@oneinabillion654 4 жыл бұрын
What is continue? Does it mean skip to next element in list?
@DivaAngelika
@DivaAngelika 4 жыл бұрын
finally i found ur comment and its realy fixed my code...thankyouuuu
@bentv6652
@bentv6652 Жыл бұрын
given_list = [7, 5, 4, 4, 3, 2, 1, -1, -2, -3, -4, -5, -7] print(given_list) #For Loop total1 = 0 for element in given_list: if element >= 0: continue else: total1 += element print(total1) #While Loop # given_list = [7, 5, 4, 4, 3, 2, 1, -1, -2, -3, -4, -5, -7] print(given_list) total2 = 0 i = 0 while True: if i < len(given_list) and given_list[i] >= 0: i += 1 continue else: if i < len(given_list) and given_list[i] < 0: total2 += given_list[i] i += 1 else: break print(total2) Thanks, CS Dojo!
@gravevoid9885
@gravevoid9885 3 жыл бұрын
a tip if u don't understand the code he is talking about then keep watching the video over and over again NEVER GIVE UP GUYZ
@Rinaa966
@Rinaa966 Жыл бұрын
#Sum of all negative numbers from a list #With while loop list = [7, 5, 4, 4, 3, 1, -2, -3, -5, -7] total = 0 i = -1 while True: if list[i] >= 0: break else: total = total + list[i] i = i - 1 print(total) #with for loop total2 = 0 for i in range(len(list)): if list[i] < 0: total2 = total2 + list[i] i = i - 1 print(total2) Result: -17
@ushiealex2295
@ushiealex2295 Жыл бұрын
this makes a lot of sense especially i =i -1, that way, we start to index from behind and we can break when we hit positive numbers as they dont meet the condition of (list[i] < 0). Thank you so much, i was struggling with that.
@elshaddaim37
@elshaddaim37 Жыл бұрын
if you want to explanation watch the next part
@codinglearner3261
@codinglearner3261 3 жыл бұрын
Coding is not as hard as I think it was!!! Good teaching!!!!! I like the while loop better, it is easier! This is how I do it...... given_list3 = [7, 5, 4, 4, 3, 1, -2, -3, -5, -7] total7 = 0 j = 0 while j < len(given_list3): if given_list3[j]
@IAmATablecloth
@IAmATablecloth 6 жыл бұрын
given_list = [7, 6, 4, 2, -3, -4, -7] tot = 0 i = len(given_list) - 1 while True: tot += given_list[i] i -= 1 if given_list[i] >= 0: break print(tot) output: -17 Cheers!
@s6l662
@s6l662 6 жыл бұрын
the result is -14
@droidzz4935
@droidzz4935 6 жыл бұрын
@@s6l662 because the list is different
@cgpriya7526
@cgpriya7526 5 жыл бұрын
Can someone explain the logic
@kimani3597
@kimani3597 5 жыл бұрын
we're initializing i to the length of the list of numbers which is 7. But since indexing starts from 0 and not 1, we have to -1 from the length of the list in order to give us 7 i.e 0 through 6. This enables us to start the addition operation from the last number value.
@fuhbigbiufb
@fuhbigbiufb 5 жыл бұрын
@@cgpriya7526 this is the i printed step by step : 6 5 4
@mazenl7139
@mazenl7139 6 жыл бұрын
given_list3 = [7, 5, 4, 4, 3, 1, -2, -3, -5, -7] total3 = 0 i = 6 while i < len(given_list3): total3 += given_list3[I] i += 1 print (total3) # This code only adds neg numbers without the need to check if element < 0 since we know where the negative numbers start. Probably the fast in terms of cpu clock cycles. Can add element < 0 to make sure element is negative but wanted to show that we can just skip over the positive values without actually checking them!
@joshhostler9135
@joshhostler9135 6 жыл бұрын
total4 = 0 i=9 while True: total4 += given_list[i] i += -1 if given_list[i] > 0: break print(total4) =-17 Thank you very much for your videos, Im new to programming and code and your videos are helping me exponentially. I do have a question in terms of the code I entered in order to get -17. The only way I could get the answer was to manually put the index number in the i=[x] box. For this particular problem the last number in the index was in the 9th position but I was wondering if there was a way to tell python to start at the end of the index instead of the beginning. Thank you again for your time and I look forward to hearing from you and your future videos.
@Kauroseta
@Kauroseta Жыл бұрын
Now I've got the same question... I know that the last integer in list is equals to -1.. i wonder how to apply that
@hanhtooaein6368
@hanhtooaein6368 Жыл бұрын
What about add the line 'given_list.reverse()' before the while loop? Is this legit?
@khaliqshaikh2392
@khaliqshaikh2392 6 жыл бұрын
it can be done simply by: given_list3 = [7, 5, 4, 4, 3, 1, -2, -3, -5, -7] total4 = 0 for element in given_list3: if element
@venxity6959
@venxity6959 2 жыл бұрын
given_list3 = [5, 4, 4, 3, 1, -2, -3, -5] total = 0 i = 0 while i < len(given_list3): if given_list3[i] < 0: total += given_list3[i] i += 1 print (total) output: -10 Here's the while loop version
@nutman2361
@nutman2361 2 жыл бұрын
@@venxity6959 im wondering why the number one is not included ?? is greater ten zero so why isnt add on the total number??
@WillChaseIV
@WillChaseIV 2 жыл бұрын
@@nutman2361 1 is greater than zero but the sum formula only applies to numbers less than zero
@nutman2361
@nutman2361 2 жыл бұрын
@@WillChaseIV oh ok thx
@santoshgareri377
@santoshgareri377 5 жыл бұрын
given_list = [7,5,4,4,3,1,-2,-3,-5,-7] i= 0; total = 0 while i < len(given_list): if given_list[i] < 0: total += given_list[i] i += 1
@azmainahnaf8490
@azmainahnaf8490 5 жыл бұрын
The solution of answer using for loops: total = 0 for element in given_list3: if element < 0: total += element The solution of answer using while loops: total = 0 i = len(given_list3) - 1 while given_list3[i] < 0: total += given_list3[i] i -= 1
@jaden3433
@jaden3433 2 жыл бұрын
Thank You so much for uploading the results I was going crazy trying to get it! 👏🏼
@joeroganpodfantasy42
@joeroganpodfantasy42 2 жыл бұрын
0:27 Shortcut to move your curser to the left by 4 is to press shift+tab as opposed to tab which move cursor to the right by 4. I kind of love watching these little shortcuts used by programmers and they dint even mention them cause it's second nature for them. But i think in that case he used backspace
@MoeDaliven
@MoeDaliven 6 жыл бұрын
this is like a game to me, but the game lately is getting REALLY hard or prob im just not smart enough
@kristijanross977
@kristijanross977 6 жыл бұрын
But it feels good when you complete the game
@MoeDaliven
@MoeDaliven 6 жыл бұрын
thanks man for this comment, i lately got bored and quit learning programming, but since your point is totally right i'll continue learning again,
@kristijanross977
@kristijanross977 6 жыл бұрын
heyyy never give up and remember you must enjoy programming because there is always something new (there's no ending) and basically you are completing the game every time you master over a new problem.
@JPAll
@JPAll 6 жыл бұрын
I'm with you. I'm watching this and I have no idea what is going on. It will take me a while to get through this
@ghost-4783
@ghost-4783 6 жыл бұрын
Just a reminder 3 months later to get back on the horse bro! Don't give up. (:
@laspalmasera
@laspalmasera 4 жыл бұрын
Using 'While Loop': given_list3 = [7, 5, 4, 4, 3, 1, -2, -3, -5, -7] total6 = 0 i = 0 while i < len(given_list3): if given_list3[i] < 0: total6 += given_list3[i] i += 1 print(total6) Using 'For Loop': given_list3 = [7, 5, 4, 4, 3, 1, -2, -3, -5, -7] total7 = 0 for element in given_list3: if element < 0: total7 += element print(total7)
@r.aniketh2542
@r.aniketh2542 5 жыл бұрын
This is of the hw list=[7,5,4,4,3,1,-2,-3,-5,-7] t=0 i=0 while i
@karthickdurai2157
@karthickdurai2157 4 жыл бұрын
our aim is to reduce the no. of iterations as it is going to increase the time complexity of our problem. so the solution is perfectly fine. But imagine we have a list of 1000 elements and only the last two are negative so we are running 998 unneccesary iterations. I hope you get my point. You could use list.reverse() to reverse it first and then probably do the same operation
@alexleebenbaum3296
@alexleebenbaum3296 4 жыл бұрын
Thank you so much for your coding answer! Could you tell me why it should put "while i
@karthickdurai2157
@karthickdurai2157 4 жыл бұрын
@@alexleebenbaum3296 he's iterating from i to length of the list; for eg: if array = [1 , 2 , 3 , 4] index starts from 0 to 3, length of this list would be 4, so we are giving i = 0 , i < len(list) which is 4 over here, so it runs from 0 to 3
@karthickdurai2157
@karthickdurai2157 4 жыл бұрын
@Jobert Alcantara yes good question in that case my friend just sort it and repeat the procedure we want to reduce the no. Of iterations as much as possible. Hope I answered your question
@tiggygao
@tiggygao 6 жыл бұрын
given_list3 = [7, 5, 4, 4, 3, 1, -2, -3, -5, -7] total6 = 0 i = 0 while i < len(given_list3): if given_list3[i] < 0: total6 += given_list3[i] i += 1 elif given_list3[i] > 0: total6 = total6 i += 1 print(total6)
@saimirfan902
@saimirfan902 4 жыл бұрын
Hey CS Dojo, I appreciate your hard work and your dedication on this subject but I would like to say this. This course is not easy to understand for someone who is absolutely beginning from the start. It is perfect for someone who is revising or trying to learn a new logic but it is in no way easy for a beginner. Too many vast topics are being covered in a matter of 10 mins and I would say that the sequence of the videos also does not make it easy for the beginner to learn python or programing. The first vid was about variables but the next video you post is about conditional statments. I mean it is more important for a beginner to learn what actually string is or what integer is, and then the next video which gets posted is about functions. I mean come on man :D I understand it is not easy out there, but for a beginner the start should always be easy. Other than that I like your other vids, you are very skilled and intelligent and i envy you sometimes :)
@bhuvanasaravanan7348
@bhuvanasaravanan7348 5 жыл бұрын
list=[7,4,4,3,5,-1,-5,-3,-5] i=0 total=0 for i in list: if i
@markdoors2103
@markdoors2103 5 жыл бұрын
Thank you CS Dojo for the Lesson. I find it great. I hope the answer below is correct using while loop. given_list4 = [7,5,4,4,3,1,-2,-3,-5,-7] total6 = 0 i = -1 while True: total6 += given_list4[i] i -= 1 if given_list4[i] >= 0: break print (total6) Answer = -17
@swallowedinthesea11
@swallowedinthesea11 5 жыл бұрын
Nice! Can you add the negatives: given_list4 = [3, -99, 7, 45, -3, 1, -8, 2, -7]
@markdoors2103
@markdoors2103 5 жыл бұрын
Hi. Yes, it is possible. just replace the list as follow: given_list4 = [3, -99, 7, 45, -3, 1, -8, 2, -7] total6 = 0 for element in given_list4: if element
@swallowedinthesea11
@swallowedinthesea11 5 жыл бұрын
@@markdoors2103 Nice! Have fun! doc.lagout.org/programmation/python/Python%20for%20Kids_%20A%20Playful%20Introduction%20to%20Programming%20%5BBriggs%202012-12-22%5D.pdf www.digitalocean.com/community/tutorials/digitalocean-ebook-how-to-code-in-python www.cs.uky.edu/~keen/115/Haltermanpythonbook.pdf
@Madvincul
@Madvincul 2 жыл бұрын
why when summing the negatives i = -1 AND when summing the positives i = 0 (and not +1) ?
@yoel-9554
@yoel-9554 4 жыл бұрын
based from what i learn from this video, i can make this given_list3=[6,5,4,4,3,1,-2,-3,-5,-7] i=0 total=0 while i
@caiocardoso6288
@caiocardoso6288 2 жыл бұрын
Total4 = 0 i = len(given_list3) - 1 while given_list3[i] < 0: Total4 += given_list3[i] i -= 1 Print(Total4) -17 This is super useful thanks!
@Fk1rainbowsix
@Fk1rainbowsix 2 жыл бұрын
That "i = len(given_list3) - 1" was smart haha
@pickolzi5312
@pickolzi5312 6 жыл бұрын
Using the for loop instead of while loop, should work. given_list = [7, 5, 4, 4, 3, 1, -2, -3, -5, -7] total = 0 for a in given_list: if a < 0: total += a print(total)
@zero_x93
@zero_x93 5 жыл бұрын
*While Statement* given_list3 = [5, 4, 4, 3, 1, -2, -3, -5, -7] Total6 = 0 I = 0 While i < len(given_3) and given_list3[i] > 0: Total6 -= given_list3[i] I += 1 Print(total6) -17 *For Loop* given_list3 = [5, 4, 4, 3, 1, -2, -3, -5, -7] Total6 = 0 I = 0 For element in given_list3: If element < 0 : Total6 += element I += 1 Print(total6) -17
@Jason0-z7v
@Jason0-z7v 4 жыл бұрын
list = [7, 5, 4, 4, 3, 1, -2, -3, -5, -7] total = 0 for element in list: if element < 0: total += element print(total)
@Ebishenmansfield
@Ebishenmansfield 4 жыл бұрын
I did List.reverse and then did using for and while. I ll try urs too. Ty
@mal1k_me
@mal1k_me 4 жыл бұрын
That's great Both of you
@ahuttee
@ahuttee 4 жыл бұрын
gl = [7, 5, 4, 4, 3, 1, -2, -3, -5, -7] total = 0 i = 0 while i < len(gl): if gl[i] < 0: total += gl[i] i += 1 print(total) gives out -17
@nrdawson27
@nrdawson27 4 жыл бұрын
Since we know it starts from the highest number it only makes sense to have it start from the back of the list. This way it goes through the least amount of numbers possible. given_list4.reverse() total7 = 0 i = 0 while True: total7 += given_list4[i] i += 1 if given_list4[i] >= 0: break print(total7)
@dcornerofficial2411
@dcornerofficial2411 2 жыл бұрын
The list.reverse() command really helped me. So, YK also said that there is a way to do this in a for loop. Whoever is having problems with it, here's the code - #given_list4 = [7, 5, 4, 4, 3, 1, -2, -3, -5, -7] given_list4.reverse() total = 0 for i in given_list4: if i >= 0: break total += i print(total) total will be -17
@jordancooney2036
@jordancooney2036 2 жыл бұрын
Here is my code for the problem he gave us at the end(it works btw): given_list = [ 7, 5, 4, 4, 3, 1, -2, -3, -5, -10] total = 0 i = 0 while i < len(given_list) and given_list[i] > 0: i += 1 while i < len(given_list) and given_list[i] < 0: total += given_list[i] i += 1 print(total)
@vaishnavij5578
@vaishnavij5578 2 жыл бұрын
Can you please explain the code
@nbgyz
@nbgyz 6 жыл бұрын
I don't think anybody did it like this but this is what I figured: # while loop 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)
@mickeychen52
@mickeychen52 6 жыл бұрын
great idea! it's easier to get the length of the list and compute backward from the last element, when there are a bunch of number to handle. thanks for sharing!
@thechannel8616
@thechannel8616 6 жыл бұрын
I thought of doing it like that but it's not exactly dynamic and it relies on knowing the number of elements in the list when he said that we should assume we don't know the number of the elements Nice solution nevertheless
@satyad6955
@satyad6955 6 жыл бұрын
This seems great. I thought of using the break function but couldn't figure out a way. One correction, instead of giving number to element we can use- "i = len(given_list3) -1".
@kimiaprh9993
@kimiaprh9993 6 жыл бұрын
Satya D That's exactly what i did!
@jodux6883
@jodux6883 5 жыл бұрын
AWESOME
@junwoojeong4886
@junwoojeong4886 Жыл бұрын
It was a bit rough, but very helpful. given_list = [7, 5, 4, 4, 3, 1, -2, -3, -5, -7] total = 0 i = 0 while i < len(given_list): if given_list[i] < 0: total += given_list[i] i += 1 print(total)
@brandonl.garcia2737
@brandonl.garcia2737 5 жыл бұрын
Here is a while one that works: given_list5 = [7, 5, 4, 4, 3, 1, -2, -3, -5, -7] total7 = 0 i = -1 while given_list5[i] < 0: total7 += given_list5[i] i -= 1 print(total7) -17
@TrungNguyen-is6lq
@TrungNguyen-is6lq 5 жыл бұрын
How can the index be -1 or lower? It starts at 0 right?
@brandonl.garcia2737
@brandonl.garcia2737 5 жыл бұрын
@@TrungNguyen-is6lq -1 will start index at the end of the list, in this case that's -7
@andarmanullang4797
@andarmanullang4797 4 жыл бұрын
​@@brandonl.garcia2737 we supposed to not knowing the content of the list
@kalsummers9555
@kalsummers9555 4 жыл бұрын
I was so close but I had first put i = 1 instead of -1. thanks to Brandon I was able to fix it after working on it and giving up. Thought I was going dumb lol. We're all in the process together! given_list3 = [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)
@farhanpirjade5594
@farhanpirjade5594 4 жыл бұрын
@@andarmanullang4797 CS dojo also said that we know the elements in the list are in descending order .
@anonymousrajat3976
@anonymousrajat3976 6 жыл бұрын
Using while loop----: given_list3 = [7, 5, 4, 4, 3, 1, -2, -3, -5, -7] # Q. Add all negative numbers excluding positive numbers total = 0 i = 0 while i < len(given_list3): if given_list3[i]
@mohitsharma4667
@mohitsharma4667 6 жыл бұрын
Your videos are really helpful! This has helped me to progress a lot in python. Your explanations are simple yet very informative.
@רומי-ג8ק
@רומי-ג8ק 2 жыл бұрын
# With the while loop list = [7, 5, 4, 4, 3, 1, -2, -3, -5, -7] total = 0 i = 0 a = True while a: if i < len(list) and list[i] len(list): a = False i +=1 print(total) #With the for loop list = [7, 5, 4, 4, 3, 1, -2, -3, -5, -7] total = 0 for element in list: if element
@hydrargyrumm
@hydrargyrumm 5 жыл бұрын
total = 0 i = -1 while True: total += given_list[i] i -= 1 if given_list[i] >= 0: break print(total)
@nirwanra
@nirwanra 5 жыл бұрын
Bhargav Kantheti dude how did u get that 2nd line (i=-1) and 5th line (i-=1) ?? I dont get it
@hydrargyrumm
@hydrargyrumm 5 жыл бұрын
@@nirwanra I basically went backwards of that list. What I did was going from back since I knew they were in descending order. So for i = -1 the list gives last number...and when I keep decrementing I move from right to left.
@ricardo81090
@ricardo81090 4 жыл бұрын
lista3 = [7,5,4,4,3,1,-2,-3,-5,-7] suma = 0 i = 0 while i
@Goordela
@Goordela 4 жыл бұрын
given_list3 = [7, 5, 4, 4, 3, 1, -2, -3, -5, -7] total3 = 0 i = -1 while True: total3 += given_list3[i] i -= 1 if given_list3[i] > 0: break print(total3)
@afsirlaghari3738
@afsirlaghari3738 4 жыл бұрын
This one worked for me. Thanks bro.
@joehan4s
@joehan4s 4 жыл бұрын
why i = -1 ? i thought this created to start the count from the list
@Goordela
@Goordela 4 жыл бұрын
Flashing Light Since the list is sorted in the descending order, if we start by the -1 index it will be the last digit which will be the last negative number. So by adding -1 to the index it will keep counting until given_list[i] > 0.
@ytlmm
@ytlmm 5 жыл бұрын
given_list3 = [7,5,4,4,3,1,-2,-3,-5,-7] i = 0 totalNeg = 0 while i < len(given_list3): if given_list3[i] < 0: totalNeg += given_list3[i] i += 1 else: i += 1 print(totalNeg) Anwser is -17
@Crazy1793
@Crazy1793 6 жыл бұрын
# practice if the sum of the negative numbers # given_list3 = [7, 5, 4, 4, 3, 1, -2, -3, -5, -7] given_list3 = [7, 5, 4, 4, 3, 1, -2, -3, -5, -7] tot6 = 0 i = -1 while True: tot6 += given_list3[i] i += -1 if given_list3[i] > 0: break print(tot6) output -17 # i starts at - 1 instead of 0 # i += -1, so that it'll will substract 1 instead of adding 1
@elsalin9557
@elsalin9557 6 жыл бұрын
nothing came oiut while i type this code
@vijaymirji3654
@vijaymirji3654 3 жыл бұрын
# while loop given_list = [7, 5, 4, 4, 3, 1, -2, -3, -5, -7] total = 0 e = 0 while e < len (given_list): if given_list[e] > 0: e += 1 else: total += given_list[e] e += 1 print (total) # for loop given_list = [7, 5, 4, 4, 3, 1, -2, -3, -5, -7] total2 = 0 i = 0 for i in given_list: if i < 0: total2 += i print (total2) Answer >>> -17
@MasteringWoodwinds
@MasteringWoodwinds 6 жыл бұрын
During this video, I wondered: what if you want to add all positive integers, but they're all jumbled up with negatives? I struggled with it for about 20 minutes and finally figured it out. Fun challenge for anyone who wants to try. I'll nest my solution below and hopefully someone can tell me if there's a better way to do it.
@MasteringWoodwinds
@MasteringWoodwinds 6 жыл бұрын
some_list = [-2, 2, -2, 2, -2, 2] e = 0 total = 0 while e < len(some_list): if some_list[e] > 0: total += some_list[e] e += 1 elif some_list[e] < 0: e += 1 print(total)
@bryanzeng8187
@bryanzeng8187 2 жыл бұрын
late reply but much easier solution using for loop totalpos = 0 alist = list(range(a,b)) for i in alist : if i > 0 : totalpos += i
@rashidulikramsami8020
@rashidulikramsami8020 4 жыл бұрын
given_list = [7,5,4,4,3,1,-2,-3,-5,-7] i = 0 total = 0 while i < len(given_list): if given_list[i]>0: i+=1 continue elif given_list[i]
@anthonymusiani3991
@anthonymusiani3991 6 жыл бұрын
given_list3 = [-1, -2, -3, -5, -7] total6 = 0 i = len(given_list3) - 1 while i >= 0 and given_list3[i] = 0 is there in case all numbers are negative so the i value doesn't go outside the index range. Thoughts?
@CJTheKitty
@CJTheKitty 2 жыл бұрын
given_list3 =[7,5,4,4,3,1,-2,-3,-5,-7] sum_neg = 0 i = 0 while i < len(given_list3): if given_list3[i]
@ericakirarodriguesterai3936
@ericakirarodriguesterai3936 4 жыл бұрын
I Used this code given_list3 = [7, 5, 4, 4, 3, 1, -2, -3, -5, -7] total6 = 0 i = 0 while i < len(given_list3): if given_list3[i]
@tristanchambers824
@tristanchambers824 5 жыл бұрын
given_list = [5,5,4,2,1,-1,-3,-4] total6 = 0 i = -1 while given_list[i] < 0 and i < len(given_list): total2 += given_list[i] i -= 1 print(total6)
@fahimgaming594
@fahimgaming594 6 жыл бұрын
Hay YK.. Thanks for awesome guidelines. I'm always waiting for your new video😁
@jainanan911
@jainanan911 6 жыл бұрын
Had some trouble yesterday. Today went back and listened closely to what the givens were for the homework. Assuming you don't know the elements, yet you know they descend from greatest to least, find sum of negative numbers. and boom! voila! than you so much for videos! given_list3 = [7, 5, 4, 4, 3, 1, -2, -3, -5, -7] total = 0 i = len(given_list3) - 1 while True: total += given_list3[i] i -= 1 if given_list3[i] > 0: break print (total) answer: 17
@jinchengzhu4392
@jinchengzhu4392 4 жыл бұрын
This is my answer using While loop: given_list3 = [7, 5, 4, 4, 3, 1, -2, -3, -5, -7] total7 = 0 i = len(given_list3)-1 while True: total7 += given_list3[i] i = i-1 if given_list3[i] > 0: break print(total7) Answer using for loop: total8 = 0 for i in given_list3: if i < 0: total8 += i print(total8)
@KagimuBrian
@KagimuBrian 6 жыл бұрын
given_list3 = [7,5,4,4,3,1,-2,-3,-5,-7] total3 = 0 i = 0 while i < len(given_list3) & given_list[i] < 0: total3 += given_list3[i] i += 1 if given_list3[i] >= 0: break print (total3)
@kaushikraghupathrunitechie
@kaushikraghupathrunitechie 6 жыл бұрын
Feel free to Check for bugs: given_list3 = [7, 5, 4, 4, 3, 1, -2, -3, -5, -7] total6 = 0 i = len(given_list3)-1 while True: total6 += given_list3[i] i = i - 1 if given_list3[i] >= 0: break print(total6) # output : -17
@renzoaldridgequiballo3686
@renzoaldridgequiballo3686 6 жыл бұрын
bro can u help me? below is my code I had an output of -17 but i got an error, why? the error is at the bottom. given_list = [7,5,4,4,3,1,-2,-3,-5,-7] total = 0 i = 0 while True: i += 1 if given_list[i]
@kaushikraghupathrunitechie
@kaushikraghupathrunitechie 6 жыл бұрын
Renzo Aldridge Quiballo As per the question we need to find out the sum of only -ve numbers in the list, so you need to iterate from back of the list and therefore you need to take the value of i one less than the size of the list and the iteration must go from end to start so you need to decrement the 'i' value every time you compute and update the total value. Then you need to get out of the loop when a positive number is encountered, so you need to use 'break' keyword.Please check my code I posted in the comments.
@swallowedinthesea11
@swallowedinthesea11 6 жыл бұрын
given_list = [7,5,4,4,3,1,-2,-3,-5,-7] total = 0 i = 0 while True: i += 1 # i is 1 because i = 0 + 1 if given_list[i]
@superbanane4768
@superbanane4768 6 жыл бұрын
I like your Idea. But this doen't work in case the list has no negatives because you're adding the last number regardless of its value. Furthermore if the list contains only negatives the while loop won't end.
@fuzzygamer7764
@fuzzygamer7764 6 жыл бұрын
Renzo Aldridge Quiballo, because the last number, that is number -7 in index 9 is True. It is less than 0. Therefore it tries to continue the loop but cannot find Index 10.
@ardasar8032
@ardasar8032 3 жыл бұрын
total = 0 i = 0 while i < len(given_list): if given_list[i] < 0: total += given_list[i] i += 1 else: i += 1 print(total)
@adityakadam652
@adityakadam652 4 жыл бұрын
given_list3 = [7, 5, 4, 4, 3, 1, -2, -3, -5, -7] total6 = 0 for x in given_list3: if x < 0: total6 += x print(total6) -17 is this solution to problem question correct?
@cx24venezuela
@cx24venezuela 4 жыл бұрын
For visual basic ; "for i=0 to 5 ", end with "next". Use do while and end with loop. You can use "for each (element) in ( list)" And "breaks" equals to "exit do/for"
@lassebak6909
@lassebak6909 6 жыл бұрын
CS Dojo can you please put the answer from the problem you gave us in this video, in the next video you upload. Just so we know if we were right :)
@suntexi
@suntexi 6 жыл бұрын
I did this and it worked. I'm sure there are other answers though: 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: break print(total5)
@atony8155
@atony8155 6 жыл бұрын
if your last given_list's element is positive, it will still add into total. check if it's less than 0 FIRST
@amanbhargava1904
@amanbhargava1904 5 жыл бұрын
@tonnyu the given condition is you have to have the list in either descending order or ascending. There has to be a negative value for these codes to work as mentioned in the video. U have to write code accordingly, u can’t just use check for positive n break statement.
@MsDestinyS
@MsDestinyS 4 жыл бұрын
@@suntexi thanks for the answer. May i know what does i = len(given_list3) -1 mean?
@suntexi
@suntexi 4 жыл бұрын
@@MsDestinyS This assigns the number of elements in given_list3 minus 1 to i which is the loop counter. For the record, 'i' is a traditional letter to denote a loop counter in other languages such as IBM's PL/1. If you need a loop within a loop, use 'j' then 'k', or 'i2' then 'i3' etc.
@hasankaya8522
@hasankaya8522 6 жыл бұрын
Thanks for everything Dojo. You are the best . :) 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) =-17
@PubertPenis
@PubertPenis 3 жыл бұрын
This is supposing you don’t know the amount of numbers in the list so putting “i=9” is wrong
@kaushikraghupathrunitechie
@kaushikraghupathrunitechie 6 жыл бұрын
+CSDojo really appreciate what you are doing. It would be really helpful if you could start a data structure and algorithm course too.
@CSDojo
@CSDojo 6 жыл бұрын
It's coming up soon! :) Currently just focusing on beginners' content, but I do want to start a data structures and algorithms course soon as well.
@kaushikraghupathrunitechie
@kaushikraghupathrunitechie 6 жыл бұрын
CS Dojo Thank you so much for what you are doing.
@gerhenry7127
@gerhenry7127 6 жыл бұрын
CS Dojo your like a juggernaut. Great work ethic. Keep them coming.
@senthilnarayanan5481
@senthilnarayanan5481 6 жыл бұрын
make it soon and give us a notes of pdf on python
@speedychicken831
@speedychicken831 4 жыл бұрын
fukin indian
@bilalhaseeb1936
@bilalhaseeb1936 4 жыл бұрын
given_list = [5 , 4 , 4 , 3 , 2 , 1 , -1 , -3 , -4, -5 , -5 , -6] total4 = 0 i = 0 while i < len(given_list): total4 += given_list[i] i += 1 print(total4) # this code adds up all elements in the list and the answer is -5
@ArunrGamer
@ArunrGamer 6 жыл бұрын
I used the for loops. given_list3 = [7, 5, 4, 4, 3, 1, -2, -3, -5, -7] total = 0 i = 0 for element in given_list3: if given_list3[i] >= 0: given_list3[i] = 0 total += given_list3[i] i += 1 print(total) #Output: -17
@joerobinson6149
@joerobinson6149 6 жыл бұрын
thank u i couldn't wrap my head around it
@Puppetjs
@Puppetjs 5 жыл бұрын
Thanks this made sense for me.
@cleopatria1346
@cleopatria1346 6 жыл бұрын
given_list2 = [5, 4, 4, 3, 1, 0, -1, -2, -4, -5] total6 = 0 i = 0 # while loops while i < len(given_list2): if given_list2[i]
@zakarysperaw6216
@zakarysperaw6216 6 жыл бұрын
my solution, but not the same values in the list or the same variables, but it adds numbers less than zero to the total a = [1, 2, 3, 4, -1, -2, -3] total2 = 0 i = 0 while i < len(a): if a[i] > 0: i += 1 elif a[i] < 0: total2 += a[i] i += 1 print(total2) outputs -6
@higher_highs
@higher_highs 6 жыл бұрын
I went the same way but now after reading other comments here I understand this is not the most elegant way to do so.
@nontontutorial5696
@nontontutorial5696 2 жыл бұрын
My answer for the homework : given_list = [5, 4, 4, 3, 1, -2, -3, -5] i = 0 total = 0 while i < len(given_list) and True: if given_list[i]
@krishnanaudiyal2778
@krishnanaudiyal2778 4 жыл бұрын
FOR LOOPS solution to the first Q-sum of positive integers (alternate solution w/o break statement) : given_list= [5,4,4,3,1,-2,-3,-5] total3=0 for i in given_list: if i>0: total3+= i print(total3) *17* The reason i put this here is because the first Q that YK gave regarding the calculation of positive integers of the list can be done in an easier way with FOR LOOPS instead of using WHILE LOOPS even though he said the latter is more efficient in such conditions. Plus, this could only work for WHILE LOOPS if the numbers are arranged in ascending or descending order but still FOR LOOPS will be more efficient regardless of the arrangement of the numbers. Then why to use WHILE LOOP only for certain arrangements when I can still use FOR LOOP efficiently there or in any condition. So im still confused regarding the primary purpose and function of the WHILE LOOPS. Is this so, or am I missing something? Some assistance would be appreciated ;)
How To Use Functions In Python (Python Tutorial #3)
14:55
CS Dojo
Рет қаралды 2,4 МЛН
If people acted like cats 🙀😹 LeoNata family #shorts
00:22
LeoNata Family
Рет қаралды 21 МЛН
This Game Is Wild...
00:19
MrBeast
Рет қаралды 181 МЛН
While loops in Python are easy ♾️
6:58
Bro Code
Рет қаралды 418 М.
Python Object Oriented Programming (OOP) - For Beginners
53:06
Tech With Tim
Рет қаралды 3,4 МЛН
Introduction to For Loops in Python (Python Tutorial #5)
10:22
CS Dojo
Рет қаралды 1,6 МЛН
Nested while Loop in Python
12:49
Neso Academy
Рет қаралды 16 М.
Classes and Objects with Python - Part 1 (Python Tutorial #9)
11:36
While Loop in Python (Perform a Task 1000000 times With Ease) #8
10:12
Learn Python OOP in under 20 Minutes
18:32
Indently
Рет қаралды 110 М.
If people acted like cats 🙀😹 LeoNata family #shorts
00:22
LeoNata Family
Рет қаралды 21 МЛН