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
  FutureWarning: The behavior of DataFrame concatenation with empty or all-NA entries i sawtooth500 3 391 Mar-22-2024, 03:08 AM
Last Post: deanhystad
  Convert string to float problem vasik006 8 3,269 Jun-03-2022, 06:41 PM
Last Post: deanhystad
  String concatenation in SQL update statement hammer 3 1,454 Feb-24-2022, 08:00 PM
Last Post: hammer
Question Problem with string and \n Falassion 6 2,616 Jun-15-2021, 03:59 PM
Last Post: Falassion
  how to deal with problem of converting string to int usthbstar 1 1,932 Jan-05-2021, 01:33 PM
Last Post: perfringo
  Concatenation ?? ridgerunnersjw 1 1,672 Sep-26-2020, 07:29 PM
Last Post: deanhystad
  string problem Mathisdlg 6 2,784 Aug-05-2020, 09:31 AM
Last Post: Mathisdlg
  Unicode string index problem luoheng 6 2,947 Nov-23-2019, 03:04 PM
Last Post: luoheng
  simple string & input problem kungshamji 5 3,581 Jun-23-2019, 03:54 PM
Last Post: kungshamji
  Combining two strings together (not concatenation) DreamingInsanity 6 3,056 Mar-29-2019, 04:32 PM
Last Post: DreamingInsanity

Forum Jump:

User Panel Messages

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