Home > Net >  Python program to give me numbers on the left with their sum on the right while having interchanged
Python program to give me numbers on the left with their sum on the right while having interchanged

Time:11-08

Python program sum

Here's the picture of the program/ how it should work, it should display 1*1 then 12$3 < 3 is the sum of 1 2.. we only got to for loop.

I have tried a lot of solutions and this is what i got to at the end, for some reason i can't copy and paste it here without the code deleting whatever i had here..Python trying to solve also the output currently is:output

please help and thanks a lot

CodePudding user response:

I would implement this idea this way:

def func(n: int):
    for i in range(1, n   1):
        nsum = 0  # Sum of numbers in nested loop.
        last = 0  # Last number added to 'nsum'.
        string = ''  # Final string which is then printed.
        for j in range(1, i   1):
            nsum    = j  # Add to total sum.
            string  = str(j)  # Add to final string.
            last   = j  # Set last number to current.
        # Decide if either asterisk (*) or dollar ($) should be included
        # in final message, after it append total sum.
        string  = ('*' if last % 2 else '$')   str(nsum)
        print(string)
 
func(6)
  • Related