Python Forum

Full Version: f string concatenation problem
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi Group, I'm new. So hopefully I'll figure out how to navigate this forum and get the help I need as I'm trying to learn python. Currently, I'm having a problem with what I believe is called f-string concatenation. I'm using pycharm to code python as I'm working my way through a book called Neural Networks from Scratch. The code I've typed out is long.... but the snippet I'm having a problem with is this:
print(f'epoch:{epoch}, ' +
                 f"acc: {accuracy: .3f}, ' +
                 f'loss: {loss: .3f}, ' +
                 f'lr: {optimizer.current_learning_rate}')
The error is:
Error:
Traceback (most recent call last): File "C:\Users\........", line 465, in <module> f'loss: {loss: .3f}, ' + TypeError: unsupported format string passed to NoneType.__format__
I think that pycharm perhaps doesn't like the way it's set up. I don't know. Can someone explain what's gone wrong or how I can fix it?

TIA.
It's telling you that instead of being a number as you suppose, one of your variables (probably loss) is None. Trying to apply a numeric format to None generates an error.
Actually, I think I understand now. It wasn't apparent at first.

Thanks.
Actually your code shows that you don't understand f-strings. There is no need of concatenation.
print(f'epoch:{epoch}, acc: {accuracy: .3f}, loss: {loss: .3f}, lr: {optimizer.current_learning_rate}')
This is assuming you fix the problem with loss being None.