0

LMTLabSep09

In my programming class we are trying to make a text-based RPG. We are required to make a dictionary and then use a list in order to have the stats be printed in a certain order (the order being the order in the list). However, I am having trouble with my "for loop", and the stats continue to print within the dictionary, and therefore in a random order each time. Basically I'm asking how can I use myList to print the stats in the order that I want?

from random import randint

def main():
#user input
name = input("Enter character name: ")
job = input("Enter character's job title: ")
age = eval(input("Enter character's age: "))
bckstry = input("Enter character's backstory: ")

#randomized stats
strength = randint (20, 30)
agility = randint (10, 20)
intel = randint (5, 50)

#character stats w/ formulas
health = strength * 10
mana = intel * 8
damage = agility * 6

#hero dictionary
myHero = {'name':name,
          'job':job,
          'age':age,
          'backstory':bckstry,
          'str':strength,
          'agi':agility,
          'int':intel,
          'health':health,
          'mana':mana,
          'dmg':damage}
print(myHero)

def callMyHero():
myHero = {'name':name,
          'job':job,
          'age':age,
          'backstory':bckstry,
          'str':strength,
          'agi':agility,
          'int':intel,
          'health':health,
          'mana':mana,
          'dmg':damage}

myList = ['name','job','age','backstory','str','agi','int','health','mana','dmg']

for i in range(0,len(myList)):
     print(myList[i])

main()
callMyHero()
Yu Hao
  • 119,891
  • 44
  • 235
  • 294
  • Off-topic: `eval(input("Enter character's age: "))` is awful, since the user can execute real code that way. You probably want something like `int()`, `float()` or `ast.literal_eval()` rather than `eval()`; all of those options avoid arbitrary code execution. – ShadowRanger Sep 11 '15 at 05:02

1 Answers1

0

You might find this question helpful here on why your dictionary is printing in an unexpected order. But to answer your question:

myList = ['name','job','age','backstory','str','agi','int','health','mana','dmg']
for key in myList:
    print myHero[key]
Community
  • 1
  • 1
TJ1S
  • 528
  • 3
  • 12