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 Respostas
+ 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.