0

What is result () in line three doing

1 n = 50 2 num_list = [10, 4, 23, 6, 18, 27, 47] 3 result = () 4 found = False 5 for n1 in num_list: 6 for n2 in num_list: 7 if(n1 + n2 == n): 8 result = (n1, n2) 9 found = True 10 break 11 if found == True: 12 break 13 print (result)

21st Dec 2019, 4:07 PM
amith
5 Antworten
+ 1
It creates an empty new tuple, because later in line 8 an assignmentbis done. But line 3 is not needed. Assignment in line 8 can be done directly.
21st Dec 2019, 5:39 PM
Lothar
Lothar - avatar
+ 1
@Lothar what is actually happening in line 4, 9 and 11
21st Dec 2019, 5:53 PM
amith
+ 1
In line 4 a Status variable is initialized with True. It will be used to manage the program flow in the following 2 nested for loops. In line 7 it is checked if a desired value is reached. If so, found will be set to True, and in the next line 'break' will leave from the inner for loop. In line 11 found is checked to True. If so, break is also leaving the outer for loop.
21st Dec 2019, 7:46 PM
Lothar
Lothar - avatar
+ 1
The code is not very complex and i gave you all information to understand what happens. Please take some time to get through all of it, it will help you more if can catch it yourself instead of asking for it.
21st Dec 2019, 7:58 PM
Lothar
Lothar - avatar
0
for what purpose we intitialized status variable found = False? in line 4 I couldn't understood why found is defined there as False. when I checked with out line 4 it resulted in an error. without an initialization variable at line 4, what will happen to line 9 @Lothar
21st Dec 2019, 7:53 PM
amith