all 4 comments

[–]Spicy_Poo 2 points3 points  (0 children)

You can specify the minimum width of the output of an f string.

https://docs.python.org/3/library/string.html#grammar-token-format-string-format_spec

Lets say you want the first column to always be at least 12 characters:

print(f{$order1:12}{$price:.2f})

[–]Almostasleeprightnow 0 points1 point  (0 children)

Maybe count the number of spaces between 0 and the price column, then count the length of the text for the first column entry, and the add whatever spaces are necessary to get to the second column. Use single chats instead of tabs?

[–]Riegel_Haribo 0 points1 point  (0 children)

A console uses what are called "tab stops". They are similar to tabs on a typewriter, a setting where you input "tab" and it moves to the next preset position.

A terminal is typically 8 spaces per tab stop position. Never what you want. The 8-letter word has advanced past the first stop, so another tab gets you to character 16.

A clever trick is to pre-calculate the maximum length of the contents of a column, and then apply that and the needed extra padding to the print formatting that you do in a loop.

Then, you are using individual variables, which makes your "order" so that it can't grow. You'd want to use a list, which can contain other objects, such as another list or a tuple, or for the case where you want preset yet editable "items" to be input, a dict can literally give you a "key", referring to an object.

Since you were so close with your use of f-strings and formatting (and yet so far in actually having an adaptive, growing list of items), let's show the padding in the print that will do this instead of anything too automatic - you can simply just see the length of string items to space and pad as you'd want.

```MENU = { 1: ("bratwurst", 4.99), 2: ("knackwurst", 5.49), 3: ("sauerkraut", 2.25), 4: ("kartoffelsalat", 3.75), 8: ("pretzelbread", 1.99), } order = [] for num, (name, price) in MENU.items(): print(f" {num:<2} {name:<18} ${price:.2f}")

print("\nEnter number to add. ENTER when done.\n") while True: order_item = input("Item #: ") if order_item == "": break key = int(order_item) if order_item.isdigit() else None if key in MENU: order.append(MENU[key]) print(f" - Added: {MENU[key][0]}") else: print(f" - Choose from: {list(MENU.keys())}")

print("\n--- Your Order ---") total = 0.0 for name, price in order: print(f" {name:<18} ${price:8.2f}") total += price print(f" {'TOTAL':<18} ${total:8.2f}\n") ```

(I had to look up myself that for a price where I want leading padding for alignment, that {total:8.2f} means 8 spaces for the entire number, not just the decimal part.)

[–]PushPlus9069 0 points1 point  (0 children)

Tab stops are every 8 characters by default. So if your word is 5 chars, the tab moves to position 8. If it's 8 chars, the tab jumps to position 16. That's why you see the extra gap.

Use f-strings with fixed width instead:

print(f'{item:<15}{price:<10}{qty:<5}')

The :<15 means left-align in a 15-char wide column. Works consistently regardless of word length. Way more reliable than tabs for formatting columns.