Type Object has no attribute

2024/11/24 18:31:57

I am working on a program, but I am getting the error "Type object 'Card' has no attribute fileName. I've looked for answers to this, but none that I've seen is in a similar case to this.

class Card:
RANKS = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13)
SUITS = ('s', 'c','d','h')
BACK_Name = "DECK/b.gif"def __init__(self, rank, suit):"""Creates a card with the given rank and suit."""self.rank = rankself.suit = suitself.face = 'down'self._fileName = 'DECK/' + str(rank) + suit[0] + '.gif'class TheGame(Frame):def __init__(self):Frame.__init__(self)self.master.title("Memory Matching Game")self.grid()self.BackImage = PhotoImage(file = Card.BACK_Name)self.cardImage = PhotoImage(file = Card.fileName)

Any help to solving this would be great. thanks.

Answer

You have three class attributes: RANKS, SUITS and BACK_Name.

class Card:# Class Attributes:RANKS = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13)SUITS = ('s', 'c','d','h')BACK_Name = "DECK/b.gif"

You haven't defined fileName as a class attribute so trying to get an attribute named fileName will raise an AttributeError indicating that it doesn't exist.

This is because fileName, or rather, _fileName has been defined as an instance attribute via self._filename:

# Instance Attributes:
def __init__(self, rank, suit):"""Creates a card with the given rank and suit."""self.rank = rankself.suit = suitself.face = 'down'self._fileName = 'DECK/' + str(rank) + suit[0] + '.gif'

To access this attribute you must first create an instance of the Card object with with c = Card(rank_value, suit_value); then you can access the _filename via c._filename.

https://en.xdnf.cn/q/120502.html

Related Q&A

Generate random non repeating samples from an array of numbers

I made a battleships game and I now need to make sure that the computer doesnt attack at the same spot twice.My idea of it is storing each shots co-ordinates in a variable which gets added to whenever …

How to get back fo first element in list after reaching the end? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.Want to improve this question? Add details and clarify the problem by editing this post.Closed 5 years ago.Improve…

Collision detection on the y-axis does not work (pygame)

I am trying to get the collision detection in my code to work. I am using vectors and I want the player sprite to collide and stop when it collides with a sprite group called walls. The problem is that…

Search for string within files of a directory

I need help writing a light weight Python (v3.6.4) script to search for a single keyword within a directory of files and folders. Currently, I am using Notepad++ to search the directory of files, altho…

Extract parent and child node from python tree

I am using nltks Tree data structure.Below is the sample nltk.Tree.(S(S(ADVP (RB recently))(NP (NN someone))(VP(VBD mentioned)(NP (DT the) (NN word) (NN malaria))(PP (TO to) (NP (PRP me)))))(, ,)(CC an…

Click button with selenium and python

Im trying to do web scraping with python on and Im having trouble clicking buttons. Ive tried 3 different youtube videos using Xpath, driver.find_element_by_link_text, and driver.find_element. What am …

Combinations of DataFrames from list

I have this:dfs_in_list = [df1, df2, df3, df4, df5]I want to concatenate all combinations of them one after the other (in a loop), like:pd.concat([df1, df2], axis=1) pd.concat([df1, df3], axis=1) p…

Python: iterate through dictionary and create list with results

I would like to iterate through a dictionary in Python in the form of:dictionary = {company: {0: apple,1: berry,2: pear},country: {0:GB,1:US,2:US} }To grab for example: every [company, country] if coun…

Jira Python: Syntax error appears when trying to print

from jira.client import jiraoptions = {server: https://URL.com} jira = JIRA(options, basic_auth=(username, password))issues = jira.search_issues(jqlquery) for issue in issues:print issueI want to print…

Matplotlib plt.xlim([x_min,x_max]), list object not callable

I want to plot a scatterplot, but set the x-label limits.axScatter = plt.subplot(111) axScatter.scatter(x=mean_var_r["Variance"],y=mean_var_r["Mean"]) xlim = [-0.003, 0.003] plt.xli…