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

13th Feb 2025, 5:44 PM
Squilliam Fancyson
3 Answers
+ 4
f-string is missing
13th Feb 2025, 5:51 PM
Mihaly Nyilas
Mihaly Nyilas - avatar
+ 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}")
13th Feb 2025, 10:01 PM
Shardis Wolfe
+ 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
14th Feb 2025, 3:07 AM
Bob_Li
Bob_Li - avatar