+ 1
How can I make this print the value instead of just the string??
So I’m trying to make a group of characters from a board game and make their data display based on user input. print("Name: {self.name}") There’s the print statement, my problem is it returns exactly what’s in the quotes, but if I move the quotes to not encompass {self.name} it results in a syntax error. I’ve included the rest of my code if it helps, but I’m stumped. https://sololearn.com/compiler-playground/csavx9wjLpLA/?ref=app
4 ответов
+ 6
f-string is missing
+ 4
Squilliam Fancyson As Mihaly pointed out, you are missing the f right before the first string quote which lets Python know it is a formatted string instead of a normal string.
Correct usage -
print(f"Name: {self.name}")
+ 3
Squilliam Fancyson
variable names are not just simple strings. you can't access variable names and call class methods simply through user input. at least not safely or without hacky eval codes.
maybe do something like this instead:
https://sololearn.com/compiler-playground/cGU8R7HI4hC1/?ref=app
+ 1
Put "f" before the first "