0

how to stop python while loop

code as below try to -1 t till t become 0 (!>0) to stop while loop but it doesn't work. import sys t = int(input()) while t > 0: for a in sys.stdin: list = [] b = a.split() for c in b: list.append(int(c)) if list[3] - list[2] == list[1] - list[0]: list.append(list[3] + (list[3] - list[2])) print(list[0],list[1],list[2],list[3],list[4]) else: list.append(list[3] * (int(list[3] / list[2]))) print(list[0],list[1],list[2],list[3],list[4]) t = t - 1

15th May 2019, 2:10 PM
adaruru
adaruru - avatar
4 Respuestas
+ 1
Well, any loop can be stopped forcefully using the "break" keyword. Perhaps you can try that?
15th May 2019, 6:59 PM
Trigger
Trigger - avatar
+ 1
1. The break keyword breaks out of the loop. 2. The "list" variable is a reserved keyword in python. You may want to rename it.
15th May 2019, 10:27 PM
inxanedev!
inxanedev! - avatar
0
unfortunately, big blocks of code like this isnt really the best. just nest for loops with range of input. for i in range(t):
16th May 2019, 1:54 AM
Choe
Choe - avatar
0
problem key: What can't stop is "for loop" not "while loop" solution: 1.add if condition to break for loop. 2.just break for loop is also work. Answer: #Eva 的回家作業 import sys #決定輸入幾次 #t = int(input()) #access console 輸入 t = int(sys.stdin.readline().strip('\n')) while t > 0: for a in sys.stdin: list = [] b = a.split() for c in b: list.append(int(c)) if list[3] - list[2] == list[1] - list[0]: list.append(list[3] + (list[3] - list[2])) print(list[0],list[1],list[2],list[3],list[4]) else: list.append(list[3] * (int(list[3] / list[2]))) print(list[0],list[1],list[2],list[3],list[4]) t = t - 1; //optional if(t==0): //optional break; t = t - 1;
8th Jun 2019, 5:05 AM
adaruru
adaruru - avatar