+ 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]

7th Oct 2020, 11:35 AM
Rohit
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
7th Oct 2020, 11:51 AM
Lothar
Lothar - avatar
+ 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.
7th Oct 2020, 11:54 AM
🇮🇳Omkar🕉
🇮🇳Omkar🕉 - avatar
+ 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.
7th Oct 2020, 3:13 PM
Lothar
Lothar - avatar
+ 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
7th Oct 2020, 3:24 PM
Lothar
Lothar - avatar
+ 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.
7th Oct 2020, 12:03 PM
Lothar
Lothar - avatar
+ 3
Your attempt?
7th Oct 2020, 11:38 AM
🇮🇳Omkar🕉
🇮🇳Omkar🕉 - avatar
+ 3
Rohit Wadher , "I tried but" - Share your code here. We'll try to explain what's wrong and how it can be fixed.
7th Oct 2020, 11:41 AM
🇮🇳Omkar🕉
🇮🇳Omkar🕉 - avatar
+ 3
items = [] n = int(input()) while True: items.append(n) print(items) if n = 0: a = items.remove(n) print(a) break
7th Oct 2020, 11:45 AM
Rohit
+ 3
7th Oct 2020, 11:52 AM
Rohit
+ 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
8th Oct 2020, 3:33 PM
Anurag Yadav
Anurag Yadav - avatar
+ 3
this worked for me! items = [] while True: n = int(input()) items.append(n) if n == 0: print(items[:-1]) break
9th Oct 2020, 2:43 AM
Greezy gree
Greezy gree - avatar
+ 3
It is a code coach problem?
9th Oct 2020, 5:51 AM
🔰Saurabh🔰
🔰Saurabh🔰 - avatar
+ 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,
9th Oct 2020, 2:23 AM
Ratnapal Shende
Ratnapal Shende - avatar
+ 2
this worked for me! items = [] while True: n = int(input()) items.append(n) if n == 0: print(items[:-1]) break
9th Oct 2020, 8:50 AM
Priyanshu Kumar
Priyanshu Kumar - avatar
+ 1
🇮🇳Omkar🕉 Thank you, i have tried it successfully.
7th Oct 2020, 11:58 AM
Rohit
+ 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
7th Oct 2020, 12:05 PM
🇮🇳Omkar🕉
🇮🇳Omkar🕉 - avatar
+ 1
🇮🇳Omkar🕉 how can I integrate an external API on WordPress blog?
7th Oct 2020, 2:18 PM
Emmanuel
+ 1
Emmanuel , I have no experience with wordpress. You can ask it as new question in qa forum. Those who know will answer 🙂.
7th Oct 2020, 2:56 PM
🇮🇳Omkar🕉
🇮🇳Omkar🕉 - avatar
7th Oct 2020, 2:58 PM
Emmanuel
+ 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
8th Oct 2020, 5:48 PM
$aiNadh
$aiNadh - avatar