Learning Python and need help moving a symbol with out disrupting the rest of the code

Viewed 69

I am learning Python and need a little help with moving my symbols over a few spaces to the left. I have completed the assignment and am not looking for homework help. I just want it to look nicer on my end. In the picture that I added in the link below, you can see that the "#" is too far to the right. I would like it to sit next to the even numbers. I have tried making more spaces in my print statements but all that does is move my entire table around. How do I get that "#" to sit right next to my even numbers?

Here is my code.

#ask for user input on size of Multiplication Table.
size = int(input("What size Multiplication Table would you like? (2-10):"))
while (size <2) or (size >10):
    print("Invalid entery! Enter a Number Between 2 and 10")
    size = int(input("What size Multiplication Table would you like? (2-10):"))   
print()
print()

#dispaly header
print("                    --- Multiplication Table(",size,"x",size,") ---")
print("  ",end="")
size += 1
for h in range(1,11):
    if h == 10:
        print("    ",h, end="  ")
    else:
        print("     ",h, end="  ")
print()
for h in range(1,100):
    print('-',end='')
print()

#display Multiplication Table
#outer loop
for a in range(1,size):
    if a ==10:
        print('',a,'|',end='')
    else:
        print('',a,' |',end='')
        
#inner loop        
    for b in range(1,size):
        result = a * b
        if result >=100:
            print(' ',result, end='   ')
        elif result >=10:
            print(' ',result, end='    ')
        else:
            print('  ', result, end='    ')
            
# for putting '#' at the end of even numbers            
        if result %2==0:
            print('#', end='')
        elif result == 100:
            print('', end='')
        else:
            print(' ', end='')
    print()
2 Answers

I've made minimal modification to your inner loop, to produce the desired effect:

    for b in range(1,size):
        result = a * b
# for putting '#' at the end of even numbers            
        if result %2==0:
            end_str='#'
        elif result == 100:
            end_str=''
        else:
            end_str=' '
        if result >=100:
            print(' ',result, end=end_str+'   ')
        elif result >=10:
            print(' ',result, end=end_str+'    ')
        else:
            print('  ', result, end=end_str+'    ')

Output, with this modification:

        1        2        3        4        5        6        7        8        9       10  
---------------------------------------------------------------------------------------------------
 1  |   1        2#       3        4#       5        6#       7        8#       9       10#    
 2  |   2#       4#       6#       8#      10#      12#      14#      16#      18#      20#    
 3  |   3        6#       9       12#      15       18#      21       24#      27       30#    
 4  |   4#       8#      12#      16#      20#      24#      28#      32#      36#      40#    
 5  |   5       10#      15       20#      25       30#      35       40#      45       50#    
 6  |   6#      12#      18#      24#      30#      36#      42#      48#      54#      60#    
 7  |   7       14#      21       28#      35       42#      49       56#      63       70#    
 8  |   8#      16#      24#      32#      40#      48#      56#      64#      72#      80#    
 9  |   9       18#      27       36#      45       54#      63       72#      81       90#    
 10 |  10#      20#      30#      40#      50#      60#      70#      80#      90#      100#   

Try this:

    for b in range(1,size):
        result = a * b

        flag = " " if result % 2 else "#"
        #put the number and the flag together
        temp = f"{result}{flag}"
        #and pad the number + flag to a constant length
        print(f'   {temp:6.6}', end="")


see my comment under the question about a clearer breakdown of how f-string formatting works.

output:

                    --- Multiplication Table( 4 x 4 ) ---
        1        2        3        4        5        6        7        8        9       10
---------------------------------------------------------------------------------------------------
 1  |   1        2#       3        4#
 2  |   2#       4#       6#       8#
 3  |   3        6#       9        12#
 4  |   4#       8#       12#      16#

see my comment about how f-string formatting works. couldn't find a really clear reference about how the formatting is specified f"{<variable>:<formatting-to-apply>}" so can't link one, but it do things like pad zeros and format floats too.

Related