So I'm supposed to output the sum 110. I solved this with easy math. Which other ways could I solve this? Just curious. :) | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

So I'm supposed to output the sum 110. I solved this with easy math. Which other ways could I solve this? Just curious. :)

sum = 99 x = 5 y = 6 print(x+y+sum)

1st Jan 2021, 11:21 PM
Caroline Eliasson
Caroline Eliasson - avatar
3 Answers
+ 3
I enjoy seeing different solutions to the same problem - just another aspect of learning for me, really. Even if my answer is correct. I agree with using another name for <sum> - I'll take that with me. I also love the list idea. Thank you both!
2nd Jan 2021, 7:22 PM
Caroline Eliasson
Caroline Eliasson - avatar
+ 2
Your method for totalling numbers could differ depending on how your numbers are presented within the code. For example, if your nums were within a list, you could do this: nums = [99, 6, 5] # list print(sum(nums)) # 110
2nd Jan 2021, 3:56 AM
Rik Wittkopp
Rik Wittkopp - avatar
+ 1
Your way is already good, simple and right, just what other ways you had in mind? I don't get it. (Edit) Use other name for <sum>, it's a built-in function name. Assignment to <sum> there will transform <sum> reference from a function object to an int object, and you get trouble when you want to use the actual <sum> function. Check this to verify sum = 99 x = 5 y = 6 print( x + y + sum ) print( sum( [ 2000, 20, 1 ] ) )
2nd Jan 2021, 3:21 AM
Ipang