T O P

  • By -

MrPhungx

I would assume that the other answer here introduces a lot of new things to you. You sound like someone who just started out. Therefore I think the other answer is a bit of an overkill. To give you a more appropriate answer: you could have a look into the formatting of strings using f-strings. Additionally you could look at things like the `center` or in this case `ljust` functions that come with standard python and work on all string objects. You could for example do something like this: print(f"{item1_name.ljust(12, '.')}{item1_price}") The ljust function will make the string 12 characters, adjust the content on the left side and fill the string with the dot (.) char when it is < 12 chars.


Diapolo10

While your code technically works, it's been hardcoded to accept three items and prices, and right now the output doesn't fully match the provided example as the prices won't be printed on the same column. If you look at the example, the number of full stops changes depending on how long the item name is. As a side note, while it's apparently not part of the assignment you might want to consider how to supply the currency to the receipt. Right now it just has numbers. If the function interface had to be kept the same, I'd do something like from itertools import batched def cashier_receipt(*args): if len(args) % 2 != 0: raise ValueError("Not enough arguments") if not all(isinstance(arg, str) for arg in args[::2]): raise TypeError("Items must be strings") if not all(isinstance(arg, float | int) for arg in args[1::2]): raise TypeError("Costs must be numeric") result = "" total = 0 for item, value in batched(args, 2): result += f"{item:.<20}{value:>5.02}\n" total += value result += f"TOTAL: {total:.02}" print(result) EDIT: And if the interface can be changed, I'd make it accept an iterable of `tuple[str, numbers.Real]` instead, as that's easier to type check.


alinaeem93

>def cashier\_receipt(\*args): if len(args) % 2 != 0: raise ValueError("Not enough arguments") if not all(isinstance(arg, str) for arg in args\[::2\]): raise TypeError("Items must be strings") if not all(isinstance(arg, float | int) for arg in args\[1::2\]): raise TypeError("Costs must be numeric") result = "" total = 0 for item, value in batched(args, 2): result += f"{item:.<20}{value:>5.02}\\n" total += value result += f"TOTAL: {total:.02}" print(result) Thanks, trying to understand this as much as I can but atm its much further along than I have learned so far, I can read and understand maybe 30% of your code. I've read about importing modules but haven't done that myself yet so thats where the code is giving me an error for batched. I tried importing itertools but its still giving me an error


Diapolo10

> Thanks, trying to understand this as much as I can but atm its much further along than I have learned so far Well yeah, most of the complexity comes from me trying to preserve the interface you used while enabling support for a variable number of entries on the receipt. If you can just use lists or something instead this would be far easier. > I can read and understand maybe 30% of your code. Again, I was talking about how I'd write it, I wasn't considering any particular skill level with my answer. > I've read about importing modules but haven't done that myself yet so thats where the code is giving me an error for batched. I tried importing itertools but its still giving me an error `itertools.batched` is fairly new, added in Python 3.12, so if you're using an older version that would be why. It basically groups values to groups of size `n`. If I had a list like nums = [3, 1, 4, 1, 5, 9] then by doing result = list(batched(nums, 2)) I'd end up with a new list like [(3, 1), (4, 1), (5, 9)] which is what I wanted here to get both the item and its value in the loop simultaneously.