#π Alignment of Code
46 messages Β· Page 1 of 1 (latest)
@frank dagger
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.
something like this. You can try the code and see what I am talking about.
do you want to do it yourself or would it be okay to use a library to get it done for you?
^ that was what I was going to say.. seems a lot easier to build a dict and pprint it
either that or tabulate to have a nice table
Yeah figured he didnt want to use pandas, is there another way to do it without loading all of pandas in?
im confused
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
Oh ok, well I am new to this so I can just follow along is that's the case.
Sounds good, we just wanted to make sure you werent trying to do it yourself on purpose
But preferably I'd want to do it myself
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
if you want to do it yourself, I just want to show you one syntax trick
Or atleast get some experience under your belt and then circle back
!e
name = "John"
print(f"{name:<10}|")
@jagged trellis :white_check_mark: Your 3.12 eval job has completed with return code 0.
John |
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
so in your example you used 10, but I am still trying to figure out what that number is doing.
notice how there is a bunch of spaces after John in the string John |
oh ok
!e
print(len("John "))
@jagged trellis :white_check_mark: Your 3.12 eval job has completed with return code 0.
10
i see
the total length of whatever input you have + the spaces will be at least 10
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.
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
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.
@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
!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}}')
@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
honestly you could just hard-code it in there for now, try to get a simple solution working first, then add more stuff
ok ill give it a shot
feel free to use my code as reference
ok thanks
so would it look something like this:
https://paste.pythondiscord.com/GOCQ
!close
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.