in Education by
If I use a while loop for my below code, it is not giving the desired output, but when i use for loop i am anle to get the output. a = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89] b = [1, 1, 2, 3, 5, 6, 7, 8, 9, 10, 11, 12, 13] x = 0 Comm = [] #common int in both lists while x in range(0,len(a)): if a[x] in b: Comm.append(a[x]) x += 1 print(Comm) Select the correct answer from above options

1 Answer

0 votes
by
 
Best answer
You need to use the for loop instead of while. If x goes from the 0 to 6, then a[6] = 13. a[6] is in b, so x becomes 7 and a[7] = 21. a[7] is not in b, so x doesn't increase. It means that x stops at 7 and doesn't break out of the while loop. Do check out Data Science with Python course which helps you understand from scratch

Related questions

0 votes
    I want to save files for each result from the loop. For example, I wrote the code but it only saves one file ... do I modify my code? Select the correct answer from above options...
asked Jan 19, 2022 in Education by JackTerrance
0 votes
    How do I use the for loop to execute the following operation? df_1=pd.concat([df_ab1,df_xy1], axis=1) df_2= ... Am I missing something? Select the correct answer from above options...
asked Jan 10, 2022 in Education by JackTerrance
0 votes
    In the python documentation, an instance of a named_tuple is created by using the below code: Point = named_tuple(' ... of doing it? Select the correct answer from above options...
asked Jan 10, 2022 in Education by JackTerrance
0 votes
    I know a little of Python and more than a year ago I wrote a small script, using pipenv to manage the ... 3.0 should work. Select the correct answer from above options...
asked Jan 19, 2022 in Education by JackTerrance
0 votes
    When I run pip3 install -r requirements.txt on the project I get this error message: pip._vendor.pkg_resources. ... on my machine. Select the correct answer from above options...
asked Jan 19, 2022 in Education by JackTerrance
0 votes
    Can anyone tell me why Python is better than R for Data Science? Select the correct answer from above options...
asked Jan 19, 2022 in Education by JackTerrance
0 votes
    I have a big data frame. I want to replace float 1.0 and 0.0 with the true and false. My code: import pandas ... to do it in one line. Select the correct answer from above options...
asked Jan 19, 2022 in Education by JackTerrance
0 votes
    Do I need coding skills for Data Science using Python? Select the correct answer from above options...
asked Jan 17, 2022 in Education by JackTerrance
0 votes
    Can anyone tell me how do I start learning Python for Data Science? Select the correct answer from above options...
asked Jan 11, 2022 in Education by JackTerrance
0 votes
    Can anyone tell me why Python is used in Data Science? Select the correct answer from above options...
asked Jan 19, 2022 in Education by JackTerrance
0 votes
    It is a principal question, regarding the theory of neural networks: Why do we have to normalize the input for ... is not normalized? Select the correct answer from above options...
asked Jan 27, 2022 in Education by JackTerrance
0 votes
    I am trying to make an app that can get the patient's symptoms as inputs and outputs the three most likely ... classes[sortedshit[2]]) Select the correct answer from above options...
asked Jan 11, 2022 in Education by JackTerrance
0 votes
    I need to find a way to clear the Listbox for the refresh function which I am making. The function should ... clearing the Listbox) Select the correct answer from above options...
asked Jan 18, 2022 in Education by JackTerrance
0 votes
    Do the Data Science course requires Python learning in-depth? Select the correct answer from above options...
asked Jan 18, 2022 in Education by JackTerrance
0 votes
    SA=1000.00 AI=0.12 MP=100.00 def remainb(x): if x==0: return 0 else: return x=(SA+(AI/12)*SA)-MP for ... . run the loop until SA==0). Select the correct answer from above options...
asked Jan 11, 2022 in Education by JackTerrance
...