0

I'm coding a little game and I want a score system. I have two variables that I want to display as "The current score is: X/X" but its not including the variables

I've tried putting it in 'quotes' put it didn't work

U = 2
L = 3
print("Current score: U/L")

I want it to be "Current score: 2/3"

martineau
  • 119,623
  • 25
  • 170
  • 301

1 Answers1

2
print(f"Current score: {U}/{L}")

Strings enclosed with simple quotes or double-quotes imply that all characters in it are just literal text - they are not interpreted as variables. From Python 3.6, the prefix f for the quotes delimiting the string denotes an special literal which can include Python expressions inside - including variables. Still, to separate these variables from plain text (and avoid unpredictable substitutions), these expressions have to be further delimited by { }, inside the string.

Prior to Python 3.6, one would have to do it in two separate steps: create a plain string with special markers were the variables contents would be inserted, and then call the .format method on the string. (or, there is an older method using the % operator instead). Fully documenting these other methods would be long - but a short form would require one to write:

print("Current score: {}/{}".format(U, L))
jsbueno
  • 99,910
  • 10
  • 151
  • 209