My guess is you have a hidden character.
I tried your code from copy and paste and got the same error but, when I typed it in it worked as expected.
I tried your code from copy and paste and got the same error but, when I typed it in it worked as expected.
class Portfolio: def __init__(self): self.holdings = {} def buy(self, ticker, shares): self.holdings[ticker] = self.holdings.get(ticker, 0) + shares def sell(self, ticker, shares): self.holdings[ticker] = self.holdings.get(ticker, 0) - shares def __iter__(self): return iter(self.holdings.items()) p = Portfolio() p.buy('Alpha', 15) p.buy('Beta', 23) p.buy('Gamma', 9) p.buy('Gamma', 20) p.sell('Beta', 5) str_len = len(max(p)[0]) for (ticker, shares) in p: if len(ticker) < str_len: spacer = ' '*(str_len - len(ticker) + 2) else: spacer = ' '*2 print(f'{ticker} {spacer} {shares}')
Output:Alpha 15
Beta 18
Gamma 29
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags