+ 2

How i print a single list..

list=[] while True: list.append(int(input())) print(list) It's print like [1] [1,5] [1,5,8] [1,5,8,10] But I want last one..

31st Oct 2020, 2:03 PM
Sid
Sid - avatar
8 Answers
+ 2
#next time pls try yourself first... Siddharth Raj list=[] try : while True: list.append(int(input())) #when there is no input, then it raise error. #except can handle that with how you want, #so I just printing to continue next.. except : print(list)
31st Oct 2020, 3:41 PM
Jayakrishna 🇼🇳
+ 7
Just change the print stmt to print(list[-1])
31st Oct 2020, 2:08 PM
Alphin K Sajan
Alphin K Sajan - avatar
+ 5
Yurii Ostapenko yeah i corrected it. Thank u. Now it works fine .
31st Oct 2020, 3:39 PM
Alphin K Sajan
Alphin K Sajan - avatar
+ 2
Alphin K Sajan indenting will not help since there is an infinite loop
31st Oct 2020, 3:37 PM
Yurii Ostapenko
Yurii Ostapenko - avatar
+ 1
If you have no input then it raise error. Use try catch blocks to work it correctly...
31st Oct 2020, 3:31 PM
Jayakrishna 🇼🇳
+ 1
You're welcome..
31st Oct 2020, 4:08 PM
Jayakrishna 🇼🇳
0
Thank u..Jayakrishna😊
31st Oct 2020, 3:51 PM
Sid
Sid - avatar
0
Just put a condition that if the user wants to stop input, enter some letter (something like 'n' ..) Then using if Condition came out from the while loop and print the list . l=list() while True : X=input() if x=='n' : break else : l.append(int(n)) print(l)
1st Nov 2020, 3:24 PM
Nikhil