#πŸ”’ Alignment of Code

46 messages Β· Page 1 of 1 (latest)

frank dagger
#

Trying to align my code properly when I enter "c" after purchasing all of my desired items. However, the alignment is pretty terrible. And I am stuck trying to print it out correctly.
https://paste.pythondiscord.com/QBTA

next rapidsBOT
#

@frank dagger

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

frank dagger
#

something like this. You can try the code and see what I am talking about.

jagged trellis
#

do you want to do it yourself or would it be okay to use a library to get it done for you?

main badger
#

^ that was what I was going to say.. seems a lot easier to build a dict and pprint it

jagged trellis
#

either that or tabulate to have a nice table

main badger
#

Yeah figured he didnt want to use pandas, is there another way to do it without loading all of pandas in?

frank dagger
#

im confused

main badger
#

So there are many modules that will take care of the printing for you, you just need to build the correct data structure and then feed that into the module. The one I refered to is pprint or pretty print

frank dagger
#

Oh ok, well I am new to this so I can just follow along is that's the case.

main badger
#

Sounds good, we just wanted to make sure you werent trying to do it yourself on purpose

frank dagger
#

But preferably I'd want to do it myself

main badger
#

So I wouldnt.. That is the benefit of python you can leverage a ton of peoples (who have WAY more experience then us) work instead of reinventing the wheel

jagged trellis
#

if you want to do it yourself, I just want to show you one syntax trick

main badger
#

Or atleast get some experience under your belt and then circle back

jagged trellis
#

!e

name = "John"
print(f"{name:<10}|")
next rapidsBOT
#

@jagged trellis :white_check_mark: Your 3.12 eval job has completed with return code 0.

John      |
jagged trellis
#

you can use f-string with the syntax :<N or :>N (N - integer) to left or right align your text, so it always has at least N total length which gets filled with spaces (there is a way to choose what the filler is as well)

#

once the user selects all items, you could try to estimate the maximum length of any of the words that need to be printed and then left-align the checkout text by that number

frank dagger
jagged trellis
#

notice how there is a bunch of spaces after John in the string John |

frank dagger
#

oh ok

jagged trellis
#

!e

print(len("John      "))
next rapidsBOT
#

@jagged trellis :white_check_mark: Your 3.12 eval job has completed with return code 0.

10
frank dagger
#

i see

jagged trellis
#

the total length of whatever input you have + the spaces will be at least 10

frank dagger
#

so i guess for my case, I would continually have to guess with what number to use for my code.

#

something to kind of play around with for a bit it seems like.

jagged trellis
#

well the number for the item column is the longest word that was selected (which you can find with a for loop)

#

then cost is at the edge of the receipt so you don't need to worry about the alignment there

#

for qty you might want to right-align depending on how many digits the sum of quantities has

frank dagger
#

so dont worry about cost?

#

right align qty hmm based on the example i am going to try something:

qty_label = "qty"
print(f"{qty_label:>10}|")
#

I know this is not right AT ALL but you said right align lol

#

i just have to figure out how to implement in my code i guess.

next rapidsBOT
#

@jagged trellis :white_check_mark: Your 3.12 eval job has completed with return code 0.

001 | Item                   | Qty
002 | ---------------------------
003 | Xbox One Controller    | 5 
004 | HP Touch Screen Laptop | 10
jagged trellis
#

!e

qty = [5, 10]
items = ['Xbox One Controller', 'HP Touch Screen Laptop']
def find_max(iterable):
    longest = 0
    for item in iterable:
        item_length = len(str(item))
        if item_length > longest:
            longest = item_length
    return longest
qty_max = find_max(qty)
items_max = find_max(items)
#print the contents using fstring
print(f'{"Item":<{items_max}} | {"Qty":>{max(qty_max, len("Qty"))}}')
print('-' * (items_max + 1), '|', '-' * (max(qty_max, len("Qty")) + 1), sep='')
for i in range(len(qty)):
    print(f'{items[i]:<{items_max}} | {qty[i]:>{qty_max}}')
next rapidsBOT
#

@jagged trellis :white_check_mark: Your 3.12 eval job has completed with return code 0.

001 | Item                   | Qty
002 | -----------------------|----
003 | Xbox One Controller    | 5 
004 | HP Touch Screen Laptop | 10
jagged trellis
frank dagger
#

ok ill give it a shot

jagged trellis
#

feel free to use my code as reference

frank dagger
#

ok thanks

frank dagger
#

!close

next rapidsBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.