Reputation: 1
I'm making a text based game, and I'm implementing a backpack using dictionary's. When I try to access a shop I made with a class there is an error which says that "buy" and "sell" are not defined, which they are. Before you have a go at me, I've looked at every query with a similar question to me already. Here is my code:
class openshop():
def buy():
t_newcoincount = 0
shoploop = 0
print("Would you like to buy a potion of strength for 2 coins(1)\nOr a potion of health for 3 coins(2)?")
while shoploop == 0:
choice = input()
if choice == "1" and backpack["Coins"] >= 2:
t_newcoincount = backpack["Coins"]-2
backpack.update({"Coins":t_newcoincount})
if "Strength Potions" in backpack.keys():
t_spcount = backpack["Strength Potions"]+1
backpack["Strength Potions"] = t_spcount
else:
backpack["Strength Potions"] = 1
shoploop = 1
elif choice == "2" and backpack["Coins"] >= 3:
t_newcoincount = backpack["Coins"]-3
backpack.update({"Coins":t_newcoincount})
if "Health Potions" in backpack.keys():
t_hpcount = backpack["Health Potions"]+1
backpack["Health Potions"] = t_hpcount
else:
backpack["Health Potions"] = 1
shoploop = 1
elif choice == "1" and backpack["Coins"] < 2:
print("You cannot afford that")
menu()
elif choice == "2" and backpack["Coins"] < 3:
menu()
else:
print("PLEASE SELECT A VALID NUMBER")
menu()
def sell():
print("What would you like to sell?")
sellchoice = input()
if sellchoice == "bronze dagger":
t_newcoincount = backpack["Coins"]+5
backpack.update({"Coins":t_newcoincount})
if "Bronze Dagger" in backpack.keys():
if backpack["Bronze Dagger"] > 1:
t_bdcount = backpack["Bronze Dagger"]-1
backpack["Bronze Dagger"] = t_bdcount
else:
del backpack["Bronze Dagger"]
elif sellchoice == "leather boots":
t_newcoincount = backpack["Coins"]+5
backpack.update({"Coins":t_newcoincount})
if "Leather Boots" in backpack.keys():
if backpack["Leather Boots"] > 1:
t_bdcount = backpack["Leather Boots"]-1
backpack["Leather Boots"] = t_bdcount
else:
del backpack["Leather Boots"]
elif sellchoice == "health potion" or sellchoice == "health potions":
t_newcoincount = backpack["Coins"]+3
backpack.update({"Coins":t_newcoincount})
if "Health Potions" in backpack.keys():
if backpack["Health Potions"] > 1:
t_bdcount = backpack["Health Potions"]-1
backpack["Health Potions"] = t_bdcount
else:
del backpack["Health Potions"]
elif sellchoice == "strength potion" or sellchoice == "strength potions":
t_newcoincount = backpack["Coins"]+2
backpack.update({"Coins":t_newcoincount})
if "Strength Potions" in backpack.keys():
if backpack["Strength Potions"] > 1:
t_bdcount = backpack["Strength Potions"]-1
backpack["Health Potions"] = t_bdcount
else:
del backpack["Strength Potions"]
menu()
def menu():
for keys,values in (backpack).items():
print("You have",values,keys)
print("Would you like to buy(1) or sell(2)?")
dec = input()
if dec == "1" or dec == "buy":
buy()
elif dec == "2" or dec == "sell":
sell()
menu()
and here are the two lines I used to access it from a different file (same folder obviously):
import Backpack
backpack.openshop()
and here is the error message I get:
Traceback (most recent call last):
File "C:\Documents and Settings\user\Desktop\Text Based Game\Backpack import test.py", line 1, in <module>
import Backpack
File "C:\Documents and Settings\user\Desktop\Text Based Game\Backpack.py", line 11, in <module>
class openshop():
File "C:\Documents and Settings\user\Desktop\Text Based Game\Backpack.py", line 110, in openshop
menu()
File "C:\Documents and Settings\user\Desktop\Text Based Game\Backpack.py", line 105, in menu
buy()
NameError: global name 'buy' is not defined
The same thing happens if I were to select "sell", just the 'buy' would be replaced will 'sell'. Please tell me what's going on and how to fix it, I'm so confused!!
Upvotes: 0
Views: 133
Reputation: 51897
I think you're confused about how classes work. Here's a basic template:
backpack.py
class Openshop:
def display(self, message):
print(message)
def buy(self):
self.display("You're buying!")
def sell(self):
self.display("Got something to sell?")
main.py
from backpack import Openshop
shop = Openshop()
shop.buy()
shop.sell()
Here are some experiments you can run with this example:
self
from the method parameters. What happens? Why?Openshop.buy()
? Why?Once you feel comfortable with this example, go ahead and start moving your existing logic into this code.
Test early and often! (also, use the REPL - it's your friend!)
Upvotes: 3
Reputation: 113
In your example, buy is (trying to be — there are other issues like a missing self argument) a method of the class openshop, so you'd have to either say something like openshop.buy(), or
x = openshop()
x.buy()
But like I said, there are many other issues and the code will not run with this one fix. I'd suggest starting with some much smaller examples to get the hang of programming with Python classes.
Upvotes: 0
Reputation: 32197
Since your buy()
method is a method of the class openshop
, you will need to call it as follows:
openshop.buy()
Upvotes: 1