How to make this code work properly | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

How to make this code work properly

i =[7,8,0,5,3,1] while True: print(i) if i ==0: continue Guys can anyone tell me how to make this code work properly I want the program to print i 7 8 5 3 1 With skipping 0 I don't know how and it prints the whole list instead of just the integrals

19th Dec 2019, 11:02 AM
Pattern
Pattern - avatar
5 Answers
+ 6
The code needs some improvement: - use for loop instead of while - print() has to be after if ... statement i =[7,8,0,5,3,1] #while True: for x in i: if x ==0: continue print(x)
19th Dec 2019, 11:37 AM
Lothar
Lothar - avatar
+ 5
here a solution with a while loop and an other one with a for loop. also some comments for you. https://code.sololearn.com/c75hGOUpS7XQ/?ref=app
19th Dec 2019, 3:53 PM
Lothar
Lothar - avatar
+ 3
Your list is named i. So if you print(i), you'll print that list. You have to index your list (using []) to print a single element, or loop over the list directly, using a for loop.
19th Dec 2019, 11:29 AM
HonFu
HonFu - avatar
+ 2
<i> is a list, so when you do print(i) Of course the whole list is printed. You need to refer to the desired list element by their index, print(i[0]) Remember indices in iterables (including list) starts with zero, so above example prints the first list element (value 7). It would have been a lot easier had you used for-loop instead of while-loop. Also note that `while True` is not a practical or preferred loop for iterating an iterable (e.g. a list).
19th Dec 2019, 11:28 AM
Ipang
+ 1
You must write "print(i)", after if clause
19th Dec 2019, 11:08 AM
Qasem