+ 2
Give hint.
End the loop when the user enters 0. Output the resulted list after the while loop ends. (using while loop) Sample Input 1 2 3 0 Sample Output [1, 2, 3]
21 Réponses
+ 16
Rohit Wadher , you may try something like this:
items = []
while True:
inp = int(input('enter a number or 0 to quit: '))
if inp == 0:
break
items.append(inp)
print(items)
you are not far away with your code. here some comments:
- your input has to be inside the for loop, otherwise you can not enter multiple times
- an indentation is missing after if... statement
- you don't need items.remove
+ 7
It can be done like this:
items = []
item = int(input())
while item!=0:
items.append(item)
item = int(input())
print(items)
We first create an empty list `items`
Then create a variable `item` and assign input to it.
We want to stop taking input when input is 0. So use condition `item! =0`. When item is 0 loop will stop. Inside loop keep appending the inputs that you get from user to the list `items`
at the end we print the list.
I'm also not python programmer so anyone please correct if I'm wrong here.
+ 5
🇮🇳Omkar🕉 , there was nothing wrong in your code 👍, i only mentioned that input() is implemented twice in that code. that is a duplication that should be avoided.
+ 5
although in code playground python 3.8 has been available for some time, it is rarely used. here is a nice sample solving the current task by using := (walrus operator)
https://code.sololearn.com/ciyLNRet035q/?ref=app
+ 4
🇮🇳Omkar🕉 , not to blame anyone, but you don't need 2 input() statements. if you switch to a while True:, you only need one input(), but to exit the loop it needs a break .
But there is nothing wrong with it.
+ 3
Your attempt?
+ 3
Rohit Wadher , "I tried but" -
Share your code here. We'll try to explain what's wrong and how it can be fixed.
+ 3
items = []
n = int(input())
while True:
items.append(n)
print(items)
if n = 0:
a = items.remove(n)
print(a)
break
+ 3
Lothar ok
+ 3
items = []
while True:
inp = int(input('enter a number or 0 to quit: '))
if inp == 0:
break
items.append(inp)
print(items)
i try my best u can check it help u r not i can say 99.9% it is correct
+ 3
this worked for me!
items = []
while True:
n = int(input())
items.append(n)
if n == 0:
print(items[:-1])
break
+ 3
It is a code coach problem?
+ 2
lst=[]
while True:
input_=int(input("Enter a number :"))
if input_ != 0:
lst.append(input_)
else:
break
print(lst)
you can do like this...
Hope this helps,
+ 2
this worked for me!
items = []
while True:
n = int(input())
items.append(n)
if n == 0:
print(items[:-1])
break
+ 1
🇮🇳Omkar🕉 Thank you, i have tried it successfully.
+ 1
Lothar ,
Thanks for trying to correct. I appreciate that.
The condition when we need to exit loop is included in the while loop itself
while item! =0
+ 1
🇮🇳Omkar🕉 how can I integrate an external API on WordPress blog?
+ 1
Emmanuel ,
I have no experience with wordpress.
You can ask it as new question in qa forum. Those who know will answer 🙂.
+ 1
🇮🇳Omkar🕉
Thanks.
+ 1
from itertools import *
lst=[]
for i in count(1):
item=int(input("Enter Choice"))
if(item==0):
break
lst.append(item)
print(lst)
#Using for loop