0
    #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.

1
  • Just return the enemy string in function encounter and save this state for later usage. Or split encounter(random.choice(enemy)) into enemy selection and encounter-call. Commented May 14, 2016 at 19:36

1 Answer 1

1

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!

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.