Hello, Back to an old problem. if you remember:
‘’’
for n in range(0, items, 1):
print(“\t\t”, list1[n], end=“\t\t\t\t “)
print(”{:.2f}\t{}”.format(float(list1[n]) * ratio, list2[n]))
‘’’
this prints: int() Float() ItemName
Now, I want: ItemName int(] Float()
The (next) problem is if item is too long, it pushes int()
and Float() to the right out of format with the rest of the lines
Here is what I do NOT want:
I tried moving list2[n] around, but did not work. There’s a way; I did for a class project in Basic. I Long time ago! Definitely do NOT remember the math. But, I do remember it took me days to figure it out! Any newer Python solutions are appreciated
If it were me, I’d maybe generate a grid of results (a list of lust of
str), then measure the width of all the strings. Make each column as
wide as its iwdest string. Allow one extra space for separation.
Then print the whole grid, suitably padded according to your
measurements.
A common approach (not as thorough as the above, and suited to
progressive output where you don’t really have the opportunity to
measure the whole grid, for whatever reason) is to pick some suitable
widths for the common “wide” widths, always print the separating space,
and just accept that really wide column values push things to the right.
You can somewhat accomodate the drift by noting when a column gets too
wide, and not padding the following columns until you’ve “not padded”
whatever excess was used by the very wide value.
This approach has the advantage that you don’t need to defer printing
until later but still keeps a space between values to aid parsing the
output and to prevent wide values abutting the following value.
If so, then you can format the space that the ‘ingredients’ occupies, based on the longest. The only other option that I can think of is to truncate the ‘ingredients’, but you may end off with something like:
for n in range(0, items, 1):
-----print(“\t\t”, list1[n], end=“\t\t\t\t “)
-----print(”{:.2f}\t{}”.format(float(list1[n]) * ratio, list2[n]))
Thanks for your reply. It’s hidden up there in my bad formatting. Let me try that again
In the variables above, cups and oats are the same list items (list2[n])
and the numbers are list1[n]. [n] is the number of items in the recipe and is the items in the loop that controls everything else. You saw what I’m getting which I don’t want…to answer your question, This output would make me happy, all the digits are lined up.
boxess oats------------ 2--------4
cups watermelon ----- 2--------4
cups figs ---------------- 2--------4
No matter how long the item in list2[n] is, it will still play nice with the others. The “-” are spaces, no time to figure out how to do spaces here. Sorry No truncating (ugly). Thanks again! I’check tomorrow… Good night.
No need to have cryptic names for Python objects: list1 means nothing. list2 means nothing. Give your Python objects meaningful names.
As above: n means nothing. If n is a number, then call it num, least ways, so that the reader has a clue, esp when there is no other code to reference.
Posting a non-working code snippet simply leaves the reader to guess at what is missing.
‘Moving the goal posts’ (changing the objective) as you have done by posting two different layouts for the output, simply adds to the confusion.
Markdown is not hard to learn; in fact it’s easier to learn than Python and makes for a far better and easier to read posting.
Never give up! ← That one is for life in general, not just for coding.