Ask your own question, for FREE!
Computer Science 14 Online
OpenStudy (anonymous):

could someone please help me convert this for loop into a while loop these are practice questions but i am so lost in how to do this. there are two for loops that needs to be converted into while loops and one while loop that needs to be converted to for loop which is number 3 1) for elt in string_list: if elt.is_upp() lower_list.append(elt.lower()) 2) ret_list = [] for i in range (2, end, 4): ret_list.append(i) if '*' in in_list[i]: ret_list[-1] = ret_list[-1]**2

OpenStudy (anonymous):

3) j=0 while i<15: if string_list[j] ='***': i +=1 j +=1 return j also i am using python language pleasee and thank you

OpenStudy (anonymous):

Answer these questions about the first one: Currently, the for loop is walking through the elements of a list of strings, checking whether each element is upper case, and if it is, making it lower case and appending it to a new list full of lower case elements. Let's say we wanted to do the same thing with a while loop (we wouldn't by the way...#1 is much easier done with a for loop). We need to create an iterator which I'll call iter. 1) Where do we want itr to start? Note that we can no longer look at each element, we have to look at the indices of the list and access the element that way. 2) When do we want the loop to stop? 3) Every time the while loop loops, what should happen to iter? If you can answer those three questions, the while loop should be pretty easy. Give it a shot, and I'll let you know if you're right.

OpenStudy (anonymous):

alright so i am trying to do this can we start from number three first because i am practising a lot in to convert while loops into for loops

OpenStudy (anonymous):

so it says convert the following while loop into a for loop. Your code should be written so that the return statement does not need to be altered. You are allowed to change the code before the while loop proper. string_list is a list of string that contains at least 15 instances of the string '***' j=0 while i<15: if string_list[j] ='***': i +=1 j +=1 return j

OpenStudy (anonymous):

alright so lets take a crack at this one, first it says while i<15 and i = i+1 so i know i ranges from (0...15) not including 15 so basically we will have like i= range(15)

OpenStudy (anonymous):

so we will have something like for i in range(15): ok i am confused i dont know what that j is doing there :s

OpenStudy (anonymous):

alright now lets try doing number 1 it says convert the following for loop into a while loop. string_list is a non zero length list that contains only strings. lower_list is an initially empty list. for elt in string_list: if elt.is_upper(): lower_list.append(elt.lower())

OpenStudy (anonymous):

#3 is, honestly, not even a proper while loop, because i is never initialized. It should have a line up top that say i=0 (or whatever). I guess let's pretend that i IS set to 0. So...your questions was "What is j there for?" Well, this loop is doing something a bit silly: it's counting the number of elements in the list there are to the point that the fifteenth '***' is reached. So for example, imagine that by the time it reaches the fifteenth '***', it also saw 7 other strings that are not '***'...the loop would return 22. But notice that j changes every single iteration, and i only changes sometimes. That definitely means you want j to be the variable that iterates, not i, because you can't control when something iterates in a for loop. So how long do we want it to run for? Well....we don't know. We know that it's going to keep going through the list of strings until it has found its fifteenth "***". So we might as well iterate through the ENTIRE list of strings, and then break out when we need it to. Your intuition was correct. We need to use a range() function. But what number goes in the function? It's not 15, because it could be 20 or 30 or 2,000,000 elements before we find the fifteenth '***'...so we should give it the length of the entire list.

OpenStudy (anonymous):

oh great i am confused lol i dont get this programming it is so tricky

OpenStudy (anonymous):

and hey my friend did this for 3 and got j = [i for i,x in enumerate(string_list) if x=='***'][15] return j i do not know what that means:s

OpenStudy (anonymous):

This way of creating list is called List Comprehension in Python http://docs.python.org/tutorial/datastructures.html#list-comprehensions

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!