Alignment problem

def listings(add = list()):
#print(add[0])
how_long =len(add)
#print(type(add[0]))
‘’’
string2 = (add[0].replace("+", “\n”))
string3 = (string2.splitlines())
#print(string3)
string4 = string3[0]
string5 = string3[1]
print(string4.rjust(6)+"\n"+"+".ljust(2)+string5.replace(" “, “”)+”\n"+"_____".ljust(5))
‘’’
for add2 in add:

    if "-" in add2:
        
        string2 = (add2.replace("-", "\n"))
        string3 = (string2.splitlines())
        #print(string3)
        string4 = string3[0]
        string5 = string3[1]
        res = int(string4) - int(string5)
        print(string4.rjust(6)+"\n"+"-".ljust(2)+string5.replace(" ", "")+"\n"+"_____".ljust(5)+"\n"+str(res).rjust(5))
            #print(add2)
        
        #print(True)
    elif "x" in add2:
        
        string2 = (add2.replace("x", "\n"))
        string3 = (string2.splitlines())
        #print(string3)
        string4 = string3[0]
        string5 = string3[1]
        res = int(string4) * int(string5)
        print(string4.rjust(6)+"\n"+"x".ljust(2)+string5.replace(" ", "")+"\n"+"_____".ljust(5)+"\n"+str(res).rjust(5))
            #print(add2)
        
        #print(True)
    elif "/" in add2:
        
        
        string2 = (add2.replace("/", "\n"))
        string3 = (string2.splitlines())
        #print(string3)
        string4 = string3[0]
        string5 = string3[1]
        res = int(string4) / int(string5)
        print(string4.rjust(6)+"\n"+"/".ljust(2)+string5.replace(" ", "")+"\n"+"_____".ljust(5)+"\n"+str(res).rjust(5))

    else:
        string2 = (add2.replace("+", "\n"))
        string3 = (string2.splitlines())
        #print(string3)
        string4 = string3[0]
        string5 = string3[1]
        res = int(string4) + int(string5)
        #print(string4, string5)
        print(string4.rjust(6)+"\n"+"+".ljust(2)+string5.replace(" ", "")+"\n"+"_____".ljust(5)+"\n"+str(res).rjust(5))

listings([“2000 + 220”,“500 - 300”,“340 + 250”, “2000 - 450”, “2500 x 600”, “500 / 2”, “10 / 3”])
#“500 - 300”

the only problem is alignment cant get the constant justification to right of numbers

You may want to check out the docs for str#format!

The basic usage is like the following:

name = 'Seth'

# Normal usage
assertEqual('My name is {0}'.format(name), 'My name is Seth')

# Can also be written like this, it's the same thing
assertEqual(f'My name is {name}', 'My name is Seth')

# >x pads the str left until it's x chars. With 10, 'Seth' gets 6 spaces in front
assertEqual(f'My name is {name:>10}', 'My name is       Seth')

# You can pad dynamically by inserting numbers into #format
length = 10
assertEquals(('My name is {:<' + str(length) + '}').format(name), 'My name is Seth      ')

The string interpolator and formatter built into Python is going to prove very handy. It’ll also keep the code clean, thus much easier to debug.

I’d also strongly recommend trying to give your variables more meaningful names.

These kinds of things do a lot to make code easier to maintain and debug, and makes it much easier for others to be able to read it and provide better support.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.