-2

There is probably an stupidly obvious solution to this but I'm new to python and can't find it. I'm working out a few of the systems for a practice project I'm working on and I can't seem to get this to work:

class Item:
    def __init__(self, name, description, type, mindamage, maxdamage):
            self.name = name
            self.desc = description
            self.type = type
            self.mindmg = mindamage
            self.maxdmg = maxdamage

woodsman = Item("'Woodsman'", "An automatic chambered in .22lr", "gun", 4, 10)
inspect = input("inspect:").lower()
print(inspect.name)
print(inspect.desc)
print(inspect.type)

I can't find a solution to this for some reason.

TheOneMusic
  • 1,776
  • 3
  • 15
  • 39
  • 1
    The input you are getting is a string, which doesn't have the attribute you're asking for. Also, I'm confused, what do you want to do with this program? – TheChicken4452 Nov 21 '21 at 00:38
  • 2
    "cant get it to work" on stack overflow its useful for you to give details: what happens? and what would you like to happen? – SamBob Nov 21 '21 at 00:41
  • @KevinHwang, how would I change the attribute to something that would work? And to answer your question, I'm working on a game and I'm trying to remake the item system – JohnMcafeeDidn'tKillHimself Nov 21 '21 at 00:42
  • 2
    Though why are there spaces between inspect and .name (etc.) also inspect is a string and doesnt have a name attribute, did you mean woodsman.name (etc)? – SamBob Nov 21 '21 at 00:43
  • 1
    It's not obvious from your code what you're trying to do. You're prompting the user for input -- what do you want to do with it? What are the types of things they might enter, and what do you want to do in response to those different values for `inspect`? – Samwise Nov 21 '21 at 00:45
  • 1
    just a suggestion- please refrain from using type in your code unless you want to utilize it. (https://stackoverflow.com/questions/10568087/is-it-safe-to-use-the-python-word-type-in-my-code) – iamakhilverma Nov 21 '21 at 00:45
  • @SamBob The program fails at the `print(inspect .name)`. I want to be able to have the user input an item, put that into a variable and then be able to get the object attributes from it. – JohnMcafeeDidn'tKillHimself Nov 21 '21 at 00:46
  • 1
    A string is a built-in datatype, you can't really change its attributes to something that would work, you would just have to change the data type you are using. – TheChicken4452 Nov 21 '21 at 00:47

3 Answers3

3

Use dataclasses and items dict:

from dataclasses import dataclass


@dataclass
class Item:
    name: str
    description: str
    item_type: str  # don't use 'type' for variables name, it's reserved name
    min_damage: int
    max_damage: int


woodsman = Item(
    name="'Woodsman'",
    description="An automatic chambered in .22lr",
    item_type="gun",
    min_damage=4,
    max_damage=10
)
# other items...

items = {
    "woodsman": woodsman,
    # other items...
}


inspect = items.get(input("inspect:").lower())
print(inspect.name)
print(inspect.description)
print(inspect.item_type)
0

This might be closer to what you're trying to do:

inventory = {
    "woodsman": Item("'Woodsman'","An automatic chambered in .22lr","gun",4,10)
}
inspect = inventory[input("inspect:").lower()]
print(inspect.name)
print(inspect.desc)
print(inspect.type)

Note that you will probably want to have some kind of error handling in case the user enters an item that doesn't exist in the inventory.

Samwise
  • 68,105
  • 3
  • 30
  • 44
-2

I was fiddling around and found another solution that works for me:

inspect = input("inspect:").lower()
exec("print(" + inspect + ".name)")
  • 1
    you *must* know this has a ton of bad coding practices, right? also, this is definitely the vaguest answer I see on this page. – rv.kvetch Nov 21 '21 at 14:26