-2

Okay, so to start off I am trying to insert a string into code that I made into string.

Here's what I mean:

x="achievement"

data= f"{x}_scores:[{ score: { type: number}},"

Trying to insert x into the the string using f-string.

But it keeps saying

NameError: name 'score' is not defined

This is a test I'm doing for a bigger project.

Note that the string I'm inserting into is code I turned into string because I need to insert 100+ words into similar strings, and I can't do it by hand. I'm just trying to generate a list and copy and paste the output elsewhere.

I absolutely need to insert it into that format. Can I get some help?

Red
  • 26,798
  • 7
  • 36
  • 58
user9583887
  • 33
  • 1
  • 7
  • 2
    Are you sure it's complaining about `{x}` and not `{ score: { type: number}}` did you try escaping the latter? – Skam Jul 24 '20 at 20:33
  • 1
    use doubled `{` and `}`: `f"{x}_scores:[{{score: {{type: number}}}},"` – Jan Stránský Jul 24 '20 at 20:34
  • @Skam That's what I'm saying. It says "score is not defined". I assume it's the "[{" characters because I tried it in a normal string statement and it worked. – user9583887 Jul 24 '20 at 20:36

1 Answers1

0

Here is how:

x = "achievement"

data = f"{x}_scores:""[{score: { type: number}},"

print(data)

Output:

achievement_scores:[{score: { type: number}},

The "" I inserted in between {x}_scores: and [{score: { type: number}}, tells python to only perform the f on the first half of the string, and then append the second half after the evaluation.

Red
  • 26,798
  • 7
  • 36
  • 58