Logic in thos code? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 5

Logic in thos code?

def func(a,b): s= 0 for i in range(a): for j in range(b): s+=(a+b)%3 return s print(func(3,4))

28th Oct 2018, 8:15 AM
AMAN TOMAR
AMAN TOMAR - avatar
6 Answers
+ 12
Loop continues 12 times (a + b) % 3 = 7%3 = 1 so s = 1*12 = 12
28th Oct 2018, 8:23 AM
Mert Yazıcı
Mert Yazıcı - avatar
+ 10
func(3,4) So, the innermost statement, s+=(3+4)%3 runs for 12 (3*4) times. (3+4)%3 = 7%3 = 1 s = 1+1+1+1+1+1+1+1+1+1+1+1 = 12 s is returned. 12 is returned. print(func(3,4)) print(12) 12
28th Oct 2018, 8:25 AM
Hatsy Rei
Hatsy Rei - avatar
+ 4
Your print(func(3,4)) command calls the func function with a = 3 and b = 4. So a+b = 7, and so (a+b)%3 = 1. Your two ‘for’ loops mean that the s+=(a+b)%3 line is executed 3*4 (=12) times, each time incrementing s by 1. All this means your function will return 12.
28th Oct 2018, 8:26 AM
Russ
Russ - avatar
+ 4
AMAN TOMAR Please, as jay told you here https://www.sololearn.com/Discuss/1557917/?ref=app try to not "call" other users in Q&A section... Here there are many users that help then try to wait instead to call determinate users after 5 minutes that you posted the question... If everyone would do it, there would no peace for some users
28th Oct 2018, 8:55 AM
KrOW
KrOW - avatar
+ 3
noted KrOW
28th Oct 2018, 11:08 AM
AMAN TOMAR
AMAN TOMAR - avatar
+ 1
AMAN TOMAR 👍👍👍
28th Oct 2018, 11:18 AM
KrOW
KrOW - avatar