Reputation: 33
def main():
name = input("What would you like to name your character?")
print("Your character has been created!")
play = Player(name)
class Player():
"""A game player"""
def __init__(self, name, items, max_items = 7):
self.__name = name
self.__items = []
self.__max_items = 7
def __str__(self):
return self.__name
def charInfo(self):
print("This is your character's name:", self.__name)
def inventory(self):
if len(self.__items) > 7:
print ("Your iventory is:")
print (self.__items)
else:
print("There's nothing in your inventory.")
def take(self, add_item):
if self.__items <= self.__max_items:
self.__items.append(add_item)
else:
print("You already have the max items in your inventory,sorry")
def drop(self, drop_item):
if drop_item not in self.__items:
print("That item isn't in your inventory.")
else:
self.__items.remove(drop_item)
choice = None
while choice != 0:
print(""" Character Menu
0 - Quit
1 - Character Information
2 - Inventory
3 - Add an item
4 - Drop an item""")
choice = input("What would you like to do?")
#Exit
if choice == "0":
print("See you later!")
#Character Information
elif choice == "1":
play.charInfo()
#Inventory
elif choice == "2":
play.inventory()
#Add an item
elif choice == "3":
add_item = input("What would you like to add to your inventory?")
add_item = add_item.lower()
play.take(add_item)
#Drop an item
elif choice == "4":
drop_item = input("What item would you like to drop?")
drop_item = drop_item.lower()
play.drop(drop_item)
main()
input("\nPress enter to exit.")
I keep getting this error message and I'm not understanding why. It looks to me as if I've defined the main(). I'm trying to see if someone can help me understand why I'm getting this error message. I'm new to python
Upvotes: 0
Views: 106
Reputation: 58
You should write main()
outside of the class Player()
.
Edit:
Your code will work this way:
def main():
name = input("What would you like to name your character?")
print("Your character has been created!")
play = Player(name)
choice = None
while choice != "0":
print(""" Character Menu
0 - Quit
1 - Character Information
2 - Inventory
3 - Add an item
4 - Drop an item""")
choice = input("What would you like to do?")
#Exit
if choice == "0":
print("See you later!")
#Character Information
elif choice == "1":
play.charInfo()
#Inventory
elif choice == "2":
play.inventory()
#Add an item
elif choice == "3":
add_item = input("What would you like to add to your inventory?")
add_item = add_item.lower()
play.take(add_item)
#Drop an item
elif choice == "4":
drop_item = input("What item would you like to drop?")
drop_item = drop_item.lower()
play.drop(drop_item)
class Player():
"""A game player"""
def __init__(self, name):
self.__name = name
self.__items = []
self.__max_items = 7
def __str__(self):
return self.__name
def charInfo(self):
print("This is your character's name:", self.__name)
def inventory(self):
if len(self.__items) > 7:
print ("Your iventory is:")
print (self.__items)
else:
print("There's nothing in your inventory.")
def take(self, add_item):
if len(self.__items) <= self.__max_items:
self.__items.append(add_item)
else:
print("You already have the max items in your inventory,sorry")
def drop(self, drop_item):
if drop_item not in self.__items:
print("That item isn't in your inventory.")
else:
self.__items.remove(drop_item)
main()
input("\nPress enter to exit.")
Upvotes: 0
Reputation: 48564
You defined main
inside the Player
class, so it only exists as a method on a Player
object (where it will be uncallable, because it takes no arguments).
You need to outdent it, to top-level, before you can call it.
Also: don't use self.__foo
for attribute names. It doesn't make them private, it's really weird to read, and it mostly just gives you grief. Use self._foo
if you must, but unless you have a really good reason to hide your state, just use self.foo
.
Upvotes: 3