1

I am trying to create ruler by print, it should look like this for input value 5:

enter image description here

Im trying to change in my code numbers to symbols, my code is:

length = str(input("Enter the ruler length = "))


def ruler(string):
    top = []
    top_out = []
    bottom = []
 
    for i in range(length):
        top.append((i+1)//10)
        bottom.append((i+1)%10)
        for i in range(length):
            if ((i+1)//10) == 0:
                top_out.append(" ")
            elif (((i+1)//10) in list(sorted(set(top)))) and (((i+1)//10) not in top_out):
                top_out.append(((i+1)//10))
            else:
                top_out.append(" ")
                print (''.join(list(map(str, top_out))))
                print (''.join(list(map(str,bottom))))
                print (string)

How to correct it to get appropriate output format of a ruler?

1

1 Answer 1

0

Ruler printing can be down by pretty small function like this,

def print_ruler(n):
    print('|....'*(n)+'|')
    print(''.join(f'{i:<5}' for i in range(n+1)))

Execution:

In [1]: print_ruler(5)
|....|....|....|....|....|
0    1    2    3    4    5    

In [2]: print_ruler(10)
|....|....|....|....|....|....|....|....|....|....|
0    1    2    3    4    5    6    7    8    9    10   

In [3]: print_ruler(15)
|....|....|....|....|....|....|....|....|....|....|....|....|....|....|....|
0    1    2    3    4    5    6    7    8    9    10   11   12   13   14   15

For double-digit numbers, It doesn't come to the center.

For ex: For 12, | align with number 1 or 2 it can't not make into the center of 12

4
  • 2
    For larger numbers, e. x. 12 ruler's number aren't under '|'. Commented Oct 14, 2022 at 21:12
  • 2
    Better add the space in the format itself: f'{i:<5}'
    – tobias_k
    Commented Oct 14, 2022 at 21:14
  • So how to correct this? I also wanted to start it from 0. Commented Oct 14, 2022 at 21:24
  • @MalumPhobos Sorry I miss read the question. updated it.
    – Rahul K P
    Commented Oct 14, 2022 at 21:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.