0
Python, infinite loops practice exercise.
Hi all, I'm stuck on the infinite loop exercise on the python core course. I'm new to coding so any help would be appreciated. so I've written: items = [] while True: n = int(input()) items.append(n) if n == 0: break print(items) which outputs what I need for the exercise but it also prints '0' but to complete it if must print. I'm not sure if I'm close or way of base. Please help
3 Answers
+ 3
Hi Luke!
It prints 0 because you're breaking the loop after adding elements to the list. Inorder to handle this problem, you can use an else statement. So, it helps you to avoid printing 0.
Here it is your working code.
items = []
while True:
n = int(input())
if n ==0:
break
else:
items.append(n)
print(items)
+ 1
It prints also '0' because you added '0' to array before break.
items = []
while True:
n=int(input())
if n==0:
break
items.append(n)
print(items)
+ 1
Great , thank you for the help!