0

I am working on a practice program where I design a function that takes a table and prints it out right justified. My code isn't the best, I am still a beginner so I don't know all the ways to make my code shorter just yet. For the actual printing part of the function, I was having trouble using the sep='' parameter in the print function. I was able to code around it but I'd like to try and understand where I went wrong that it didn't work.

Here's my code (it currently only works on a list that is 3x3):

 table = [['Koda','Lucy','Sokka'],
         ['Red','Black','Grey'],
         ['Dog','Dog','Cat']]


lengths = [] # must make global variable so value will be added with each iteration of for loop

for d in range(len(table)): # 3 times
    for a in range(len(table[d])):
        x,y,z = table[d][0:3]
        if len(x) > len(y):
            if len(x) > len(z):
                longest = len(x) # order here does matter. cannot assign function as variable
                break
            elif len(x) < len(z):
                longest = len(z)
                break
            else:
                longest = len(x)
                break
        elif len(x) < len(y):
            if len(y) > len(z):
                longest = len(y)
                break
            elif len(y) < len(z):
                longest = len(z)
                break
            else:
                longest = len(y)
                break
        else:
            if len(x) == len(z):
                longest = len(x)
                break
            elif len(x) < len(z):
                longest = len(z)
                break
            else: 
                longest = len(x)
                break
    lengths.append(longest)
    

for i in range(len(table)):
    a = table[0][i].rjust(lengths[0])
    b = table[1][i].rjust(lengths[1])
    c = table[2][i].rjust(lengths[2])
    print(a+' '+b+' '+c)

The very last print line, I tried to type as:

print(a+b+c,sep=' ') 

But it printed:

 Koda  RedDog

Where the a variable was justified and a seeming space added but no space was added between b and c.

Is my syntax slightly off or does it have to do with the flow using indexes instead of straight integers? I was able to find a way around it but I think understand why it didn't work, especially if its something I did wrong would be extremely helpful.

Also, if anyone had any hints to help make my code work for a list of any size, I will graciously accept them.

tripleee
  • 175,061
  • 34
  • 275
  • 318
  • this may save you a lot of trouble: https://pyformat.info/ . run this for example: `name = "Koda"; print(f"{name:<10}"); print(f"{name:>10}"); print(f"{name:^10}")` – hiro protagonist Oct 26 '20 at 15:41
  • @hiro That website is out of date. Using f-strings is much better... Also, it doesn't relate to the question. The question is not how to seperate them with spaces (they've already achieved that), they want to use the `sep` kwarg of the `print` function to do it. – Seth Oct 26 '20 at 15:46
  • @SethPeace i thought they had added f-strings. but you are correct they are missing (my example above is with f-strings). the rest is still correct though... yes, they achieved right-alignment - but overly complicated! that is why i commented and did not post an answer. – hiro protagonist Oct 26 '20 at 15:48
  • Your code to get the max of the length of each sublist is rather tortured. With the help of https://www.geeksforgeeks.org/python-unzip-a-list-of-tuples/ I got it down to `lengths = list(map(max, [[len(y) for y in x] for x in zip(*table)]))` – tripleee Oct 26 '20 at 16:23
  • @tripleee do you have any other good resources for understanding the use of the * ? What does it actually do? My understanding currently is that instead of calling entire list as a list, the * calls each individual item in the list and runs them through the function separately instead of as one whole list. A shortcut instead of using splices. Is this correct? I tried to make some codes to test my theory but they were not working out. – sarah.andromeda315 Oct 26 '20 at 20:10
  • Pretty much yes. See https://stackoverflow.com/questions/36901/what-does-double-star-asterisk-and-star-asterisk-do-for-parameters – tripleee Oct 27 '20 at 05:31

2 Answers2

3

I think you want to separate the variables by commas, not plus signs. + concatenates, but , puts them as separate arguments to print, which print can separate with spaces.

Seth
  • 2,214
  • 1
  • 7
  • 21
1

Try:

print(a, b, c, sep=' ')

It separates variables entered to the print function.

tripleee
  • 175,061
  • 34
  • 275
  • 318
lunesco
  • 126
  • 3
  • 9