I'm trying to make a choose your own adventure, and I want to save each scenario as a part of a list, for example "scenariolist" would consist of
scenariolist = ['you come to a path split', 'you come to another path split', 'you come to a third path split', etc.]
which the function would then pull up the right scenario based on the length and last number of another list that saves the gamestate.
example
print scenariolist[len(gamestate)]
which, if the player is on the third path split, would print
"you come to a third path split"
what I want to do is be able to have a variable saved in the string with the rest of the line, so like
"Playername comes to a path split".
if playername is set to "bob", when the list element is printed, I want to have it print
"bob comes to a path split"
rather than
"playername comes to a path split"
How do I save it so the variable remains callable after being pulled from the list rather than just printing the variable name?
CodePudding user response:
Prompt for the player's name and save the input in a variable named player_name. Then concatenate the player_name with the path string.
player_name = input()
paths = ['you come to a path split', 'you come to another path split', 'you come to a third path split']
for path in paths:
print(player_name " " path)
Output:
bob you come to a path split
bob you come to another path split
bob you come to a third path split
CodePudding user response:
Try:
scenariolist = ['{playername} comes to a path split', '{playername} comes to another path split', '{playername} comes to a third path split']
playername = 'Bob'
scenarioindex = 0
print(scenariolist[scenarioindex].format(playername=playername))
(where scenarioindex
is replaced with something like your len(gamestate)
as needed)
References used: https://docs.python.org/3/library/stdtypes.html#str.format
CodePudding user response:
Hello you can use python string formatting as follows :
my_variable = "bob"
my_string = "Hi I am {name} nice to meet you!"
print(my_string.format(name=my_variable))
# "Hi I am bob nice to meet you!"
Some documentation here : https://docs.python.org/3/tutorial/inputoutput.html#the-string-format-method