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.

15th Apr 2021, 10:50 PM
Spark TJ
Spark TJ - avatar
4 Réponses
+ 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
16th Apr 2021, 1:07 AM
Ayush Kumar
Ayush Kumar - avatar
+ 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)
16th Apr 2021, 1:43 AM
Amir
Amir - avatar
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)
15th Apr 2021, 11:44 PM
Amir
Amir - avatar
0
The append method doesn't return value Only changes the list.
15th Apr 2021, 11:53 PM
Amir
Amir - avatar