infinate loops | Sololearn: Learn to code for FREE!

0

infinate loops

How can I print resulted list after a while loo ends? Sample Input: 1 2 3 0,and Sample Output: [1,2,3]. My code: items = [] while True: n = int(input()) if n == 0: break else: print(items.append(n)) I don't know how to solve this problem, please help. Thanks.

4/15/2021 10:50:27 PM

Spark TJ

4 Answers

New Answer

+2

Correction: n = input().split() lst = [] for i in n: if i == '0': break else: lst.append(i) print(i) print(lst) Amir please read the question once again. You hadn't worked on task given by Spark TJ or else add [::-1] to the print of your code

+2

丹ⓨㄩک廾 Thank you for reminding you. 😃 I'm not good in English. I changed it. items = [] inputs= input().split(' ') n=len(inputs)-1 while True: print(inputs[n]) items.append(inputs[n]) n=n-1 if n==0: items.append(inputs[n]) print(inputs[n]) break print(items)

0

You need to remove input from the loop. Otherwise, each time you run the loop, it will ask you for input. items = [] n = int(input()) while True: if n == 0: break else: print(n) items.append(n) #or insert for sorted #items.insert(0,n) n=n-1 print(items)

0

The append method doesn't return value Only changes the list.