0

if i have the code:

attRoll = 34
hit = False
dmg = 1

attSequence = [attRoll, hit, dmg]

print attSequence[dmg]

it prints the value of hit (False) and not dmg (1), i can work around this by entering:

print attSequence[dmg+1]

my questions:
-why doesn't it print the value of dmg?
-is there a clean way (without the +1) to retrieve the value of hit?

thanks!

1 Answer 1

2

attSequence is a list, and dmg is 1, so attSequence[1] means "Give me the item at index 1 (the second item) in attSequence". For what you're doing, you probably want a dictionary (usually called a map in other languages), which stores key/value mappings, so you could store the damage with a key of "dmg":

att = {
    "roll": attRoll,
    "hit": hit,
    "dmg": dmg
}
print att["dmg"]

You can also put literals in a map, so you could do this:

att = {
    "roll": 34,
    "hit": False,
    "dmg": 1
}
Sign up to request clarification or add additional context in comments.

3 Comments

Unless roll, hit, and dmg are defined, that's not going to work. (Better to use a class for this anyway, but, the simple solution is to use "roll" etc.) I'm sure you meant to use quotes for that though :P
@2rs2ts Right you are. I think I've been doing too much JavaScript.. I agree that a class may be better, but based on my own learning experience, it'll take time before the advantages become obvious, especially in a language like Python, where classes are just glorified maps anyway..
@LennartWijers You probably want to look at the updated version. My old one would appear to work (since roll, hit, and dmg were defined), but wouldn't do quite what you expect.

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.