Python Forum
f string concatenation problem - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: f string concatenation problem (/thread-34117.html)



f string concatenation problem - growSeb - Jun-28-2021

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.


RE: f string concatenation problem - bowlofred - Jun-28-2021

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.


RE: f string concatenation problem - growSeb - Jun-28-2021

Actually, I think I understand now. It wasn't apparent at first.

Thanks.


RE: f string concatenation problem - buran - Jun-28-2021

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.