Python Forum
Problems with understanding class index.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Problems with understanding class index.
#1
class Store(): Money = 5.00 Day = 1 Stores = [] def __init__(self,StoreName,StoreProfit,storecost): self.StoreName = StoreName self.StoreCount = 0 self.StoreProfit = StoreProfit self.StoreCost = storecost def BuyStore(self): whichStore = int(input("Which store do you wish to buy?")) store = Store.Stores[whichStore-1] if store.StoreCost <= Store.Money: store.StoreCount+=1 Store.Money -= store.StoreCost else: print("You do not have enough money") @classmethod def DisplayGameInfo(cls): print("--------------------") print("Day # "+str(cls.Day)) print("Money = $"+str(cls.Money)) i = 1 for store in cls.Stores: store.DisplayStoreInfo(i) i+=1 def DisplayStoreInfo(self,i): print("Store name # "+ str(self.StoreName)) print("Money = $"+str(Store.Money)) print("Store Count " + str(self.StoreCount)) print(str(i)) def NextDay(self): Store.Day+=1 DailyProfit = self.StoreProfit * self.StoreCount Store.Money += DailyProfit Store.Stores.append(Store('Lemonade Stand',1.5,3)) Store.Stores.append(Store('Record Store',5,15)) Store.Stores.append(Store('Ice Cream Shop',10,90)) while True: Store.DisplayGameInfo() print("Available options N, B, Q") result = input("Please enter your selection") if result == 'B' or result == 'b': Store.Stores[0].BuyStore() elif result == 'N' or result == 'n': Store.Stores[0].NextDay() elif result == 'Q' or result == 'q': break else: print("Bad input") print("Thank you for playing Python Idle Tycoon")
The problem is with the logic inside of of the while loop. Store.Stores[0].BuyStore() and Store.Stores[0].NextDay() Why is the index 0 for Stores - wouldn't that just call the first class everytime? Though, when testing it - it doesn't follow. It doesn't seem to change anything no matter what the index is. I'm afraid I don't understand what's happening here. Secondly. How is Store.Stores[0].NextDay() working at all. I understand you're calling the class store and then the class variable that's a list, but what does that list have to do with NextDay which is its own instance method?

If this is TL;DR How is this working? Primarily the '0' Index? From my understanding it should just access the Lemonade Stand everytime.
if result == 'B' or result == 'b': Store.Stores[0].BuyStore() elif result == 'N' or result == 'n': Store.Stores[0].NextDay()
Reply
#2
It is not well written code.

BuyStore() and NextDay() should be a class methods. They don't use an instance variables. Since they are essentially class methods it doesn't matter which instance of Store is used to call them.

I also don't like that you have to explicitly add stores to Store.Stores. This should happen automatically in the __init__ method.
Brainbit likes this post
Reply
#3
I agree with @deanhystad This code is a mess
And there is serious problem with NextDay and how it calculates DailyProfit - it uses only StoreProfit for store at index 0 (the store on which instance the method is called), i.e. 5 while e.g. IceCream Store has DailyProfit 0f 10
Brainbit likes this post
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#4
Yeah, I should've probably prefaced that I was following a course. The instructor pretty much put it like this but talked about the problems in the QandA thread. I believe I have a better grasp on what is actually going on. I just paused the video because I assumed that the code was not going to work as intended(as you've stated) Nonetheless, I appreciate the response despite the spaghetti code. It was fixed later down the line as well.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Understanding Class Variables vindo 9 6,883 Jun-05-2019, 08:04 PM
Last Post: Yoriz
  i have problems understanding 2 functions,look for help. Tony 2 3,813 Dec-05-2018, 12:35 PM
Last Post: Tony

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020
This forum uses Lukasz Tkacz MyBB addons.
Forum use Krzysztof "Supryk" Supryczynski addons.