0

This is the code I currently have and what I want to do is take the input str. (EnemyFighting) and I want to search and print all the items inside of a dictionary that has the same name as what the player inputted.

EnemyFighting = input('Out of the enemies on the list which one do you want to fight?  \n')

        for item in EnemyList:
            if EnemyFighting.title() == item:
                print('Succesfully chosen ' + EnemyFighting.title())
                EnemySuccesfullyFound = True
                print(EnemySuccesfullyFound)
                break
            else:
                EnemySuccesfullyFound = False
       

        if EnemySuccesfullyFound == True:

            for key, value in (EnemyFighting.title()).items():
                print(key, + ': ' + str(value))

I'm doing this because I want to display the stats of the enemy before you start fighting (It's a terminal game btw)

  • What's the question? – Michael Ruth May 21 '22 at 00:14
  • Welcome to Stack Overflow. In your own words, where the code says `(EnemyFighting.title()).items()`, what do you expect that to mean? In particular: what kind of thing do you expect `EnemyFighting` to be? What kind of thing should result from `(EnemyFighting.title())`? Why should it be possible to get `.items()` from that? – Karl Knechtel May 21 '22 at 00:14
  • "I want to search and print all the items inside of a dictionary that has the same name as what the player inputted." *Where is this dictionary supposed to come from*? What should happen if the input *doesn't* match the name of any dictionary? What if it matches the name of a dictionary *that you don't want the player to know about* (something used to implement the program logic*? – Karl Knechtel May 21 '22 at 00:16
  • It took a while to understand what you are talking about, but please see the linked duplicate. Referring to variables "by name" has the same problem as creating them. While it can be done in limited contexts, it is dangerous and rarely what you actually want. Instead, let those dictionaries be the values in another dictionary, and look them using the player's input as a key. That will also simplify the check for a valid enemy name, since you can do the usual `if key in mydict:` thing instead of iterating through a list. – Karl Knechtel May 21 '22 at 00:17
  • There are multiple enemies with dictionaries that the player could 'Fight' and I would like to search through the dictionary that is basically the same as the input and right it out in a list to show the 'stats' of the enemy (Don't exactly know how to say it) – Riley Brown May 21 '22 at 00:21

0 Answers0