Hello everyone | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Hello everyone

list=[12,1,2,6] t=18 sum=0 for i in range (len(list)+1): for j in list: if t==sum+j: print (j) # j=j+1 #print (j) Tell me why it's not working.

16th Jul 2022, 1:04 PM
Akash Gupta
Akash Gupta - avatar
8 Answers
+ 7
the condition: *if t == sum + j:* will never get True. so no output is given. the variable *sum* is always 0. maybe if you use this variable, do a summing with variable *i* in each iteration and then store the result in the *sum* variable, the condition can get True
16th Jul 2022, 1:19 PM
Lothar
Lothar - avatar
+ 5
Akash Gupta , since i have no helpful task description, you have to find out by yourself where and what to do with the variable *sum*
16th Jul 2022, 1:37 PM
Lothar
Lothar - avatar
+ 2
Akash Gupta Pls describe the task the code should do. We can't fix code we don't know the purpose of. Also, know we should not fix, but help you to fix the code. This is what makes you learn for real.
16th Jul 2022, 5:26 PM
Emerson Prado
Emerson Prado - avatar
+ 1
Emerson Prado task is that you have a list and in the list we have following numbers and you have also a target value then what you want to do is that you want print the value in given list which is equal to target. Like Suppose: list=[12,3,1,6,8,8] target_value=10 Output: 3 1 6
16th Jul 2022, 11:23 PM
Akash Gupta
Akash Gupta - avatar
+ 1
Do you mean print a set of values from given list which sum equals target?
17th Jul 2022, 1:17 AM
Emerson Prado
Emerson Prado - avatar
0
Lothar can you correct this
16th Jul 2022, 1:24 PM
Akash Gupta
Akash Gupta - avatar
0
Lothar how will i iterate j by adding the value in list.
16th Jul 2022, 1:25 PM
Akash Gupta
Akash Gupta - avatar
0
Lothar I'm confused very much please solve that
16th Jul 2022, 1:53 PM
Akash Gupta
Akash Gupta - avatar