0
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
3 Answers
+ 4
f-string is missing
+ 2
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}")
+ 2
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