+ 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
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)
+ 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
+ 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.
+ 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).
+ 1
You must write "print(i)", after if clause