Accessing items within a class in Python? - python-3.x

I am very new to Object oriented programming and I am having trouble accessing items in my class when I run my main method. My program is trying to allow a user to add item prices to a cart until they are finished and prints the number of items and total.
class CashRegister:
print("Welcome to shopping world!")
def __init__(self, price):
self.price = price
def addItem(self, price):
CashRegister.totalPrice = CashRegister.totalPrice + price
CashRegister.itemCount = CashRegister.itemCount + 1
#property
def getTotal(self):
return totalPrice
#property
def getCount(self):
return itemCount
def main():
selection = "Y"
while selection != "N":
selection = input("Would you like to add another item to the
cart Y or N")
selection = selection.upper()
if selection == "Y":
price = input("What is the price of the item?")
CashRegister.addItem(price)
else:
print(CashRegister.getCount)
print(CashRegister.getTotal)
print(selection)
main()
Here is the error I am getting when I select yes:
TypeError: addItem() missing 1 required positional argument: 'price'
Here is the output I am getting when I select no:
Welcome to shopping world!
Would you like to add another item to the cart Y or Nn
<property object at 0x0000022CFFCA2598>
<property object at 0x0000022CFFCA2548>
N

first, you don't use the class name for declaring variables in its methods: you have self for that (which you can rename to whatever you like, but 'self' is convention)
second, you have to initialize your class object in the main function, otherwise Python won't know what to do with the methods you call (when you define a method in a class, the first argument self stands for the class object, so each time you initialize an object and then call a method on that, the argument you pass inside the brackets is actually the second argument, first one being the object itself)
third: this is more of a style thing, but you don't really use CamelCase in python except for names of classes, all the rest is in snake_case
fourth: += is more readable and faster than example = example + 1
class CashRegister(object) :
def __init__(self) :
self.total_price = 0
self.item_count = 0
def add_item(self, price) :
self.total_price += int(price)
self.item_count += 1
def get_total(self) :
return self.total_price
def get_count(self) :
return self.item_count
def main() :
register = CashRegister()
selection = True
while selection :
selection = input("Would you like to add another item to the cart Y or N\n\t").upper()
if selection == "Y" :
price = input("What is the price of the item?\n\t")
register.add_item(price)
else :
print(register.get_total())
print(register.get_count())
print(selection)
selection = False
main()
this is how I would probably do it, I've taken out the #property decorators because I don't know if you really have a need for them there, you can just call the methods with brackets () at the end to get what you want
Then, there's a bunch more stuff you should do if you really want to to use this, and that would be exception catching, determine how the cash register behaves if a negative value is passed as price, and so on... good luck and enjoy Python

you have many mistakes you need the determine totalprice and itemcount in self, you need to determine a variable with cashregister class
class CashRegister:
print("Welcome to shopping world!")
def __init__(self):
self.totalPrice=0
self.itemCount=0
def addItem(self, price):
self.totalPrice = self.totalPrice + price
self.itemCount = self.itemCount + 1
#property
def getTotal(self):
return self.totalPrice
#property
def getCount(self):
return self.itemCount
def main():
selection = "Y"
box=CashRegister()
while selection != "N":
selection = input("Would you like to add another item to thecart Y or N\n\t:")
selection = selection.upper()
if selection == "Y":
price = input("What is the price of the item?\n\t:")
box.addItem(int(price))
else:
print(box.getCount)
print(box.getTotal)
print(selection)
main()

Related

Function not Defined in Class

I am having trouble with a coding project in which I am trying to use classes in python to make a card game (cheat). However, when creating the player class, one of the functions that was previously defined within the class is shown as undefined. I cannot figure out the reason, and any suggestion is appreciated. Below is the definition of the classes
class card(object):
def __init__(self,rank,suit):
self.rank = rank
self.suit = suit
class player(object):
def __init__ (self):
self.number = number
self.hand = list()
#Here, hand is a list of the card class that was distributed with a suit and a rank
def check_card(self,player_rank,player_suit):
for card in self.hand:
if card.rank == player_rank and card.suit == player_suit:
return True
break
return False
def play_card(self):
suit = input('what is the suit?')
rank = input('what is the rank?')
if check_card(self,rank,suit):
print(True)
else:
print(False)
Here is the actual code that will run it
player = player()
player.play_card()
The following error was received:
NameError: name 'check_card' is not defined
I have been troubleshooting and looking at different solutions, including moving the functions outside the class, but it continues to display the same error. Can anyone point out the mistake? Thanks!
You have the following two issues in your code
The way you passed self to the check_card function is wrong. You must call it in this way
self.check_card(rank,suit)
The second issue is that the number is not defined. Thus I passed it as an argument while initializing the player. Feel free to make changes for that.
This is the corrected code :
class card(object):
def __init__(self,rank,suit):
self.rank = rank
self.suit = suit
class player(object):
def __init__ (self, number):
self.number = number
self.hand = list()
#Here, hand is a list of the card class that was distributed with a suit and a rank
def check_card(self,player_rank,player_suit):
for card in self.hand:
if card.rank == player_rank and card.suit == player_suit:
return True
break
return False
def play_card(self):
suit = input('what is the suit?')
rank = input('what is the rank?')
if self.check_card(rank,suit):
print(True)
else:
print(False)
player = player(3)
player.play_card()
Output :
what is the suit?spade
what is the rank?3
False
Based on this document the function call in python class is self.xxxx(args) (xxxx is denoted function name)
therefore the correct version of play_card function is shown as following.
enter code here
def play_card(self):
suit = input('what is the suit?')
rank = input('what is the rank?')
if self.check_card(rank,suit):
print(True)
else:
print(False)

Error : list.remove(x) : x not in list, I don't understand why

I'm working on a little text-based game and having a problem with removing element from a list.
Here is a code to run that throws the error ValueError: list.remove(x): x not in the list but I don't see why.
LVL = 'lvl'
DAMAGE = 'damage'
Items = {
'Sword':{
LVL : 1,
DAMAGE : 5,
},
'Wand':{
LVL : 1,
DAMAGE : 3,
},
}
class player():
def __init__(self):
self.inventory = []
class item():
def __init__(self, name: str, **kwarg):
self.name = name
self.dmg = kwarg.get(DAMAGE)
self.lvl = kwarg.get(LVL)
def __str__(self):
return self.name
if __name__ == "__main__":
user = player()
for i in Items.keys():
it = item(i, **Items[i])
user.inventory.append(it)
# check user's inventory
print('Inventory after append items :')
for i in user.inventory:
print(i)
# let's say i want the user to drop items
items_name = [i for i in Items.keys()]
items_to_drop = [item(i, **Items[i]) for i in items_name]
for i in items_to_drop:
user.inventory.remove(i)
My guess is that even if the items from user.inventory are the same as those in items_to_drop, the program sees it as two different variables. In which case I do not see how to perform what I want, that is removing items from user.inventory given a list filled with items to remove (because I cannot loop over user.inventory directly right ?)
I apologize if this question has been answered before. I have searched for it but with no success.
In the end, as suggested in comment, I had to overwrite __eq__() method of class item so that Python sees if two same items are actually in both user.inventory and items_to_drop. It works !
If needed, I simply did
def __eq__(self, other):
return self.name == other.name
One easy solution:
# let's say i want the user to drop items
items_name = [i for i in Items.keys()]
items_to_drop = [item(i, **Items[i]) for i in items_name]
for i in items_to_drop:
try:
user.inventory.remove(i)
except:
continue
another one and better one:
# let's say i want the user to drop items
items_name = [i for i in Items.keys()]
items_to_drop = [item(i, **Items[i]) for i in items_name]
invetory_copy = inventory.copy()
for i in inventory:
if i in items_to_drop:
inventory_copy.remove(i)
inventory = inventory_copy

Attributes as function arguments

I have defined a class named Score
class Score():
def __init__(self, player, dealer):
self.player = player
self.dealer = dealer
score = Score(31,27)
After this I defined a function named change_score which performs arithmetic operation on the value of attributes of the score object.
def change_score(i, attribute):
if i > 0:
score.attribute += 4
else:
score.attribute -= 1
return score.attribute
However when i pass an attribute like this:
change_score(0, player)
The following error pops up: NameError: name 'player' is not defined
How should i do it then?
A better method going about this would be to add the function to the class so that it makes it easier to manage, and you also need to define the attribute in the init() method making it and instance attribute
So the new code would be:
class Score:
def __init__(self, player, dealer): # add an the attribute here if you want
self.player = player
self.dealer = dealer
self.attribute = 0 # Then set that attribute parameter here (if you want)
def change_score(self, i):
if i > 0:
self.attribute += 4
else:
self.attribute -= 1
return self.attribute
Outside of the Class add:
score = Score(31, 27)
score.change_score(-1)

how to access to data from a class which is stored in another class in python?

This is my code. I got a problem when i want to print the information inside the class 'pokemon'
class trainer(object):
def __init__(self, name, pokemons = [], money = 0):
self.name = name
self.pokemons = pokemons
self.money = money
this is my first class which has every pokemon per trainer
class pokemon(object):
def __init__(self, name, attribute, attacks = {}, health = '==========='):
self.name = name
self.attribute = attribute
self.health = health
self.attacks = attacks
The other class where I take the pokemon to import to the other class
class fight():
def __init__(self, fighter1, fighter2):
self.fighter1 = fighter1
self.fighter2 = fighter2
def fighting(self):
if len(Trainer1.pokemons) >= 1 and len(Trainer2.pokemons) >= 1:
print('{} wanna fight against {}'.format(Trainer1.name, Trainer2.name))
keepgoing = True
print('{} got this Pokemons: '.format(Trainer1.name))
i = 0
for i in Trainer1.pokemons:
print(i)
#while (keepgoing):
else:
print('You gotta have pokemons to fight')
return False
I thought that creating a class named fight for getting in battle would be the most wise idea but I'd like to know another method to do it
Pokemon1 = pokemon('Charizard', 'Fire', attacks={'1':'ball fire', '2':'cut', '3':'fire blast', '4':'mega kick'})
Pokemon2 = pokemon('Charmander', 'fire', attacks={'1':'blast', '2':'scratch', '3':'heat', '4':'tear'})
Trainer1 = trainer('Santiago', pokemons=[Pokemon1, Pokemon2])
Pokemon3 = pokemon('Charizard', 'Fire', attacks={'1':'ball fire', '2':'cut', '3':'fire blast', '4':'mega kick'})
Pokemon4 = pokemon('Charmander', 'fire', attacks={'1':'blast', '2':'scratch', '3':'heat', '4':'tear'})
Trainer2 = trainer('Alejandra', pokemons=[Pokemon3, Pokemon4])
Okay my problem is in the class fight. when i want to print the names of the pokemons i get the following message:
Santiago got this Pokemons:
<__main__.pokemon object at 0x000002AAD9B64D00>
<__main__.pokemon object at 0x000002AAD9B92DF0>
i know that the pokemon class has various instances, but how can i access to them?
To make your life easier, I recommend that you implement the __str__ dunder method on pokemon. This will resolve the issue that you are seeing right now, and make future prints of pokemon much easier.
That would look something like this:
class pokemon(object):
def __init__(self, name, attribute, attacks = {}, health = '==========='):
self.name = name
self.attribute = attribute
self.health = health
self.attacks = attacks
def __str__(self):
return "Pokemon: %s (Health: %11s)" % (self.name, self.health)
When you print the 'Charmander' pokemon, it'll look something like this:
Pokemon: Charmander (Health: ===========)
Of course, you can change the return of the __str__ to return whatever you want out of the pokemon.

Sorting a dictionary that contains a class holding 5 variable by one of the class variables

I have a dictionary that contains keys that are made from a class containing 5 variables. I want to sort this dictionary by one of the class vars.
here is what i currently have
class Player:
def __init__(self,name,wins,losses,ties,winpercent):
self.__name = name
self.__wins = wins
self.__losses = losses
self.__ties = ties
self.__winpercent = winpercent
# mutators
def setname(self,name):
self.__name = name
def setwins(self,wins):
self.__wins = wins
def setlosses(self,losses):
self.__losses = losses
def setties(self,ties):
self.__ties = ties
def setwinpercent(self,winpercent):
self.__winpercent = winpercent
# accessors
def getname(self):
return self.__name
def getwins(self):
return self.__wins
def getlosses(self):
return self.__losses
def getties(self):
return self.__ties
def getwinpercent(self):
return self.__winpercent
def displayHighScores(self):
print("\n"," "*2,self.__name," "*(24-len(self.__name)),self.__wins)
def displayplayers(self):
print(self.__name)
I store Players like this:
def addplayer(players):
newName = input("\nEnter new Player name or 9 to quit: ")
wins = "0"
losses = "0"
ties = "0"
winpercent = "0"
if not newName:
print("\nNo name detected try again")
addplayer(players)
elif newName == '9':
print("\nReturning to Options menu")
else:
players[newName] = Player(newName,wins,losses,ties,winpercent)
saveData(players)
return players
Finally i am working on a sorted hi scores list. right now i can print my dictionary unsorted like this:
def printhiscores(players):
print("\n"," "*13,"HiScores")
print(" "*3,"Name"," "*20,"Wins")
if len(players) == 0:
print("\nNo current Players in memory.")
else:
for x in players.keys():
players[x].displayHighScores()
DisplayHighScores () being a part of the class object.
I have been reading on dictionary sorting using
OrderedDict(sorted(players.items(), key=itemgetter(1)))
but this returns edit:TypeError: '<' not supported between instances of 'Player' and 'Player'
Again I am looking to sort my dictionary of players by their win attribute and then print this new order to a high score screen. Any help would be greatly appreciated. I will post if i make any more progress on my own on this.
Your Player instances are not orderable however, as they don't implement comparison methods:
TypeError: '<' not supported between instances of 'Player' and 'Player'
If you wanted to sort them by the value returned, say, wins, then just access that information instead of just returning the value:
OrderedDict(sorted(players.items(), key=lambda kv: kv[1].getwins()))
Otherwise, give your Player class rich comparison methods; methods by which to determine when one instance should be sorted before or after another. You'll also need an equality test. It's easier to just implement support for < (lower then) and equality, then use the #functools.total_ordering decorator to provide the rest:
from functools import total_ordering
#total_ordering
class Player:
def __lt__(self, other):
if not isinstance(other, Player):
return NotImplemented
return self.__wins < other.__wins
def __eq__(self, other):
if not isinstance(other, Player):
return NotImplemented
return self.__wins == other.__wins
The above makes two Player instances equal when their wins are equal, and orders them by that attribute. You'll need to adjust this for your application as needed.

Resources