Python Forum
f string concatenation problem
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
f string concatenation problem
#1
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.
buran write Jun-28-2021, 04:54 AM:
Please, use proper tags when post code, traceback, output, etc. This time I have added tags for you.
See BBcode help for more info.
Reply
#2
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.
Reply
#3
Actually, I think I understand now. It wasn't apparent at first.

Thanks.
Reply
#4
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.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Question Problem with string and \n Falassion 6 520 Jun-15-2021, 03:59 PM
Last Post: Falassion
  how to deal with problem of converting string to int usthbstar 1 571 Jan-05-2021, 01:33 PM
Last Post: perfringo
  Concatenation ?? ridgerunnersjw 1 630 Sep-26-2020, 07:29 PM
Last Post: deanhystad
  string problem Mathisdlg 6 1,186 Aug-05-2020, 09:31 AM
Last Post: Mathisdlg
  Unicode string index problem luoheng 6 1,150 Nov-23-2019, 03:04 PM
Last Post: luoheng
  simple string & input problem kungshamji 5 1,443 Jun-23-2019, 03:54 PM
Last Post: kungshamji
  Combining two strings together (not concatenation) DreamingInsanity 6 1,373 Mar-29-2019, 04:32 PM
Last Post: DreamingInsanity
  Problem with inserting a string in to Sqlite db darktitan 3 2,269 Mar-03-2019, 06:30 PM
Last Post: stranac
  IP string manipulation problem TheRealNoob 12 3,087 Feb-04-2019, 09:29 AM
Last Post: perfringo
  problem in replace string into file Saeid_Bibak 1 1,040 Jan-20-2019, 09:46 PM
Last Post: Gribouillis

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020