Q:

Using previous random string in python

    #Variables
enemy=['Dummy','Ghost','Warrior','Zombie','Skeleton']

#Functions
#Meeting an enemy - Attack / Defend Option
def encounter(enemy):
    print(name,"encountered a",enemy,".","What do you do?")
    print("Attack? or Defend?")

#def defend(dvalue):

def battle():
    encounter(random.choice(enemy))
    #Attack Or Defend?
    choice=input("What do you do?")
    if choice!="Attack": #If the choice isn't attack then ask again
        print("Do you attack or defend?")
        choice=input("What do you do?")
    if choice!="Defend": #If the choice isn't defend then ask again
        print("Do you attack or defend?")
        choice=input("What do you do?")
    #Say correct sentence depending on what you do.
    dmg=randint(0,50) #Dmg randomizer
    if choice=="Attack": #If the choice was attack then do a random number of dmg to it
        print(name,choice,"s",enemy,".","You deal",dmg,"damage","to it.")
    if choice=="Defend": #If ... to it
        print(name,choice,"s.")

I am making a text based RPG game. All is going well but I have one problem which I don't know how to solve and haven't found any solutions for. Basically when you encounter an enemy it chooses a random one from the list. 'Bob has encounter a skeleton.' That's fine but then when it does the damaged part it prints the entire list and I don't know how to make it so that is prints the previous enemy selected, in this case a skeleton.
Any solution would be appreciated. Thanks.

A:

You could save the choice of enemy to a variable:
current_enemy = random.choice(enemy)
encounter(current_enemy)
# ...
if choice=="Attack":
  print(name,choice,"s",current_enemy,".","You deal",dmg,"damage","to it.")
# etc...

Hope that helps!

