Python Forum

Full Version: First Day using Python. NEED Simple Math CODE HELP!
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
can someone help me with a simple *for yous* lines of code..

I need to input a number of no more then 10 or 11 digits. say 01234567890

then need too add them #+#+#+#+#+#+#+#+#+# Then ##+##+##+##+##+## THen ###+###+###+### up too ############

then multiply the results by 8. like say 24 = 8, 23=7 22=6. I know its probly stupid easy but im still learning. and show the different multi's for eash group. i dunno if that explain enough but if you could make me a something too which you may think im needing that would greatful :)

Jesse.
What have you tried?
Please show your code and ask a specific question. Nobody is going to "make you something". This forum is for answering questions, not doing your homework.

Some parts of your description are confusing. I assume #+#+#+#+#+#+#+#+#+# should be 0+1+2+3+4+5+6+7+8+9+0 given your example input of 01234567890, but I have no idea what you mean by ##+##+##+##+##+##? Should that be 01+23+45+67+89+0? What about ###+###+###+###? Is that 012+345+678+90?

So you sum all these paired sums up and multiply by 8. That is clear, but I have no idea what you mean by this: like say 24 = 8, 23=7 22=6
number = "392130"





digit_sum = sum(int(digit) for digit in number)

print("Digit sum:", digit_sum)





group_size = 2

digit_groups = [int(number[i:i+group_size]) for i in range(0, len(number), group_size)]

group_sum = sum(group for group in digit_groups)

print("Group sum (size 2):", group_sum)



group_size = 3

digit_groups = [int(number[i:i+group_size]) for i in range(0, len(number), group_size)]

group_sum = sum(group for group in digit_groups)

print("Group sum (size 3):", group_sum)





group_size = 4

digit_groups = [int(number[i:i+group_size]) for i in range(0, len(number), group_size)]

group_sum = sum(group for group in digit_groups)

print("Group sum (size 4):", group_sum)





nums = [8,7,6,5,4,3,2,1]

for num in digit_groups:

  closest = min(nums, key=lambda x: abs(x-num))

  print(num," Closest multiple of 8 is :",closest)

(Jan-11-2023, 08:37 PM)menator01 Wrote: [ -> ]What have you tried?

im not asking for work but yea 12+34+56+78 123+456+789 but can't so past 8.. so like 12 would be 4 , turst me im very VERY new to this.. im trying to learn just for this simple but needful task...


so i can input a number up too 11 digits and willl count 1+2+3+4+5+6 12+34+56 and 123+456 .. so say 12+34+56 = 6 thats what i need.. UGH... sorry i know you code guys can be Fussy with instructions from a guy like me.. :(
Indeed, you will find that computers need very specific instructions, and so do we if we are trying to help. So, to clarify:
In your first set of numbers (392130) the first part would be 3+9+2+1+3+0 = 18, correct? I don't understand where 1+2=4, so me that is 3.
Next calculation 39+21+30 = 90, correct? If not, how are you arriving at your "sums"?
Third sum similar, but what happens when you are grouping the digits 4 at a time? With six digist, how are you handing that there will not be 4 digits left for the second group - or more importantly, what do your instructions say you need to do?