Python: Accessing List Elements From Other Class Methods
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
I'm trying to access the 6th element in my list (from a different class method) using a for loop.
This is what the list would consist of:
personalInfo = ['firstName', 'lastName', 'email', 'password', 'birthdate', 'alias']
And this is the code that I am running to try and get the alias, thus leading to me needing to get the password:
edit: Just decided to include the whole class in case I miss anything small.
class Account:
accountInfo = {}
def __init__(self):
choice = raw_input("Would you like to login or signup?n")
if choice.lower() == "login":
self.login()
elif choice.lower() == "signup":
print "Great! Fill in the following."
self.signup()
else:
self.__init__()
def signup(self):
accountID = '%010x' % random.randrange(16**10)
personalInfo =
firstName = raw_input("First Name: ")
lastName = raw_input("Last Name: ")
email = raw_input("E-Mail: ")
password = raw_input("Password: ")
birthdate = raw_input("DOB (DD/MM/YYYY): ")
alias = raw_input("Username/Alias: ")
personalInfo.append(firstName)
personalInfo.append(lastName)
personalInfo.append(email)
personalInfo.append(password)
personalInfo.append(birthdate)
personalInfo.append(alias)
self.accountInfo[accountID] = personalInfo
self.personalInfo = personalInfo
print self.accountInfo
def login(self):
self.alias = raw_input("Username/Alias: ")
for i in self.personalInfo:
if self.alias == self.personalInfo[5]:
self.password = raw_input("Password: ")
if self.password == True:
print "You have successfully logged on."
else:
self.password
else:
self.password
print self.alias, self.password
And this is the error I'm getting:
File "liveShare.py", line 122, in login
for i in self.personalInfo:
AttributeError: Account instance has no attribute 'personalInfo'
Also, I apologize if some of my code doesn't make sense logically. I'm new to this, so there are probably some obvious mistakes but please rip me to shreds criticism wise. I want to learn the correct way.
All help is appreciated :)
python list class-method
|
show 1 more comment
I'm trying to access the 6th element in my list (from a different class method) using a for loop.
This is what the list would consist of:
personalInfo = ['firstName', 'lastName', 'email', 'password', 'birthdate', 'alias']
And this is the code that I am running to try and get the alias, thus leading to me needing to get the password:
edit: Just decided to include the whole class in case I miss anything small.
class Account:
accountInfo = {}
def __init__(self):
choice = raw_input("Would you like to login or signup?n")
if choice.lower() == "login":
self.login()
elif choice.lower() == "signup":
print "Great! Fill in the following."
self.signup()
else:
self.__init__()
def signup(self):
accountID = '%010x' % random.randrange(16**10)
personalInfo =
firstName = raw_input("First Name: ")
lastName = raw_input("Last Name: ")
email = raw_input("E-Mail: ")
password = raw_input("Password: ")
birthdate = raw_input("DOB (DD/MM/YYYY): ")
alias = raw_input("Username/Alias: ")
personalInfo.append(firstName)
personalInfo.append(lastName)
personalInfo.append(email)
personalInfo.append(password)
personalInfo.append(birthdate)
personalInfo.append(alias)
self.accountInfo[accountID] = personalInfo
self.personalInfo = personalInfo
print self.accountInfo
def login(self):
self.alias = raw_input("Username/Alias: ")
for i in self.personalInfo:
if self.alias == self.personalInfo[5]:
self.password = raw_input("Password: ")
if self.password == True:
print "You have successfully logged on."
else:
self.password
else:
self.password
print self.alias, self.password
And this is the error I'm getting:
File "liveShare.py", line 122, in login
for i in self.personalInfo:
AttributeError: Account instance has no attribute 'personalInfo'
Also, I apologize if some of my code doesn't make sense logically. I'm new to this, so there are probably some obvious mistakes but please rip me to shreds criticism wise. I want to learn the correct way.
All help is appreciated :)
python list class-method
ifpersonalInfois defined within theAccountclass, then you could just define it asself.personalInfo = ['firstName', 'lastName', 'email', 'password', 'birthdate', 'alias'].
– John Anderson
Nov 25 '18 at 3:01
3
There seems to be some context missing. What is the for loop even for? You never usei.
– gilch
Nov 25 '18 at 3:03
@JohnAnderson I tried that but it didn't seem to work. I could have forgotten within editing thought, so I'll give it a try again.
– H4MMY
Nov 25 '18 at 3:04
@gilch Yeah, I tried to keep it concise as well as I thought it would be enough to answer. I can edit the rest in if I was wrong. And as far as theiin myforloop, it's the only way I've been taught to represent "each item" in the list.
– H4MMY
Nov 25 '18 at 3:07
What we really want is an mvce. Try to reduce your code to illustrate the problem only, but completely.
– gilch
Nov 25 '18 at 3:10
|
show 1 more comment
I'm trying to access the 6th element in my list (from a different class method) using a for loop.
This is what the list would consist of:
personalInfo = ['firstName', 'lastName', 'email', 'password', 'birthdate', 'alias']
And this is the code that I am running to try and get the alias, thus leading to me needing to get the password:
edit: Just decided to include the whole class in case I miss anything small.
class Account:
accountInfo = {}
def __init__(self):
choice = raw_input("Would you like to login or signup?n")
if choice.lower() == "login":
self.login()
elif choice.lower() == "signup":
print "Great! Fill in the following."
self.signup()
else:
self.__init__()
def signup(self):
accountID = '%010x' % random.randrange(16**10)
personalInfo =
firstName = raw_input("First Name: ")
lastName = raw_input("Last Name: ")
email = raw_input("E-Mail: ")
password = raw_input("Password: ")
birthdate = raw_input("DOB (DD/MM/YYYY): ")
alias = raw_input("Username/Alias: ")
personalInfo.append(firstName)
personalInfo.append(lastName)
personalInfo.append(email)
personalInfo.append(password)
personalInfo.append(birthdate)
personalInfo.append(alias)
self.accountInfo[accountID] = personalInfo
self.personalInfo = personalInfo
print self.accountInfo
def login(self):
self.alias = raw_input("Username/Alias: ")
for i in self.personalInfo:
if self.alias == self.personalInfo[5]:
self.password = raw_input("Password: ")
if self.password == True:
print "You have successfully logged on."
else:
self.password
else:
self.password
print self.alias, self.password
And this is the error I'm getting:
File "liveShare.py", line 122, in login
for i in self.personalInfo:
AttributeError: Account instance has no attribute 'personalInfo'
Also, I apologize if some of my code doesn't make sense logically. I'm new to this, so there are probably some obvious mistakes but please rip me to shreds criticism wise. I want to learn the correct way.
All help is appreciated :)
python list class-method
I'm trying to access the 6th element in my list (from a different class method) using a for loop.
This is what the list would consist of:
personalInfo = ['firstName', 'lastName', 'email', 'password', 'birthdate', 'alias']
And this is the code that I am running to try and get the alias, thus leading to me needing to get the password:
edit: Just decided to include the whole class in case I miss anything small.
class Account:
accountInfo = {}
def __init__(self):
choice = raw_input("Would you like to login or signup?n")
if choice.lower() == "login":
self.login()
elif choice.lower() == "signup":
print "Great! Fill in the following."
self.signup()
else:
self.__init__()
def signup(self):
accountID = '%010x' % random.randrange(16**10)
personalInfo =
firstName = raw_input("First Name: ")
lastName = raw_input("Last Name: ")
email = raw_input("E-Mail: ")
password = raw_input("Password: ")
birthdate = raw_input("DOB (DD/MM/YYYY): ")
alias = raw_input("Username/Alias: ")
personalInfo.append(firstName)
personalInfo.append(lastName)
personalInfo.append(email)
personalInfo.append(password)
personalInfo.append(birthdate)
personalInfo.append(alias)
self.accountInfo[accountID] = personalInfo
self.personalInfo = personalInfo
print self.accountInfo
def login(self):
self.alias = raw_input("Username/Alias: ")
for i in self.personalInfo:
if self.alias == self.personalInfo[5]:
self.password = raw_input("Password: ")
if self.password == True:
print "You have successfully logged on."
else:
self.password
else:
self.password
print self.alias, self.password
And this is the error I'm getting:
File "liveShare.py", line 122, in login
for i in self.personalInfo:
AttributeError: Account instance has no attribute 'personalInfo'
Also, I apologize if some of my code doesn't make sense logically. I'm new to this, so there are probably some obvious mistakes but please rip me to shreds criticism wise. I want to learn the correct way.
All help is appreciated :)
python list class-method
python list class-method
edited Nov 25 '18 at 3:17
H4MMY
asked Nov 25 '18 at 2:58
H4MMYH4MMY
36
36
ifpersonalInfois defined within theAccountclass, then you could just define it asself.personalInfo = ['firstName', 'lastName', 'email', 'password', 'birthdate', 'alias'].
– John Anderson
Nov 25 '18 at 3:01
3
There seems to be some context missing. What is the for loop even for? You never usei.
– gilch
Nov 25 '18 at 3:03
@JohnAnderson I tried that but it didn't seem to work. I could have forgotten within editing thought, so I'll give it a try again.
– H4MMY
Nov 25 '18 at 3:04
@gilch Yeah, I tried to keep it concise as well as I thought it would be enough to answer. I can edit the rest in if I was wrong. And as far as theiin myforloop, it's the only way I've been taught to represent "each item" in the list.
– H4MMY
Nov 25 '18 at 3:07
What we really want is an mvce. Try to reduce your code to illustrate the problem only, but completely.
– gilch
Nov 25 '18 at 3:10
|
show 1 more comment
ifpersonalInfois defined within theAccountclass, then you could just define it asself.personalInfo = ['firstName', 'lastName', 'email', 'password', 'birthdate', 'alias'].
– John Anderson
Nov 25 '18 at 3:01
3
There seems to be some context missing. What is the for loop even for? You never usei.
– gilch
Nov 25 '18 at 3:03
@JohnAnderson I tried that but it didn't seem to work. I could have forgotten within editing thought, so I'll give it a try again.
– H4MMY
Nov 25 '18 at 3:04
@gilch Yeah, I tried to keep it concise as well as I thought it would be enough to answer. I can edit the rest in if I was wrong. And as far as theiin myforloop, it's the only way I've been taught to represent "each item" in the list.
– H4MMY
Nov 25 '18 at 3:07
What we really want is an mvce. Try to reduce your code to illustrate the problem only, but completely.
– gilch
Nov 25 '18 at 3:10
if
personalInfo is defined within the Account class, then you could just define it as self.personalInfo = ['firstName', 'lastName', 'email', 'password', 'birthdate', 'alias'].– John Anderson
Nov 25 '18 at 3:01
if
personalInfo is defined within the Account class, then you could just define it as self.personalInfo = ['firstName', 'lastName', 'email', 'password', 'birthdate', 'alias'].– John Anderson
Nov 25 '18 at 3:01
3
3
There seems to be some context missing. What is the for loop even for? You never use
i.– gilch
Nov 25 '18 at 3:03
There seems to be some context missing. What is the for loop even for? You never use
i.– gilch
Nov 25 '18 at 3:03
@JohnAnderson I tried that but it didn't seem to work. I could have forgotten within editing thought, so I'll give it a try again.
– H4MMY
Nov 25 '18 at 3:04
@JohnAnderson I tried that but it didn't seem to work. I could have forgotten within editing thought, so I'll give it a try again.
– H4MMY
Nov 25 '18 at 3:04
@gilch Yeah, I tried to keep it concise as well as I thought it would be enough to answer. I can edit the rest in if I was wrong. And as far as the
i in my for loop, it's the only way I've been taught to represent "each item" in the list.– H4MMY
Nov 25 '18 at 3:07
@gilch Yeah, I tried to keep it concise as well as I thought it would be enough to answer. I can edit the rest in if I was wrong. And as far as the
i in my for loop, it's the only way I've been taught to represent "each item" in the list.– H4MMY
Nov 25 '18 at 3:07
What we really want is an mvce. Try to reduce your code to illustrate the problem only, but completely.
– gilch
Nov 25 '18 at 3:10
What we really want is an mvce. Try to reduce your code to illustrate the problem only, but completely.
– gilch
Nov 25 '18 at 3:10
|
show 1 more comment
2 Answers
2
active
oldest
votes
I thinks you can use this method to get access to the attributes of another Class
class Foo:
def __init__(self, args):
self.personalInfo = args
def print_Info(self):
print self.personalInfo
foo = Foo(['firstName', 'lastName', 'email', 'password', 'birthdate', 'alias'])
print_Info(foo)
add a comment |
You don't show the Account class's init method (should be right after the class declaration), but make sure you clearly define the variable name in the arguments. This will ensure it is named correctly and passed to following methods.
def __init__(personalInfo=):
self.personalInfo = personalInfo
..
#then later (or in another script that imports Account):
acct = Account(['John', 'Doe', 'email', 'password', '1/1/99', 'JohnDoe Alias'])
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53464280%2fpython-accessing-list-elements-from-other-class-methods%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
I thinks you can use this method to get access to the attributes of another Class
class Foo:
def __init__(self, args):
self.personalInfo = args
def print_Info(self):
print self.personalInfo
foo = Foo(['firstName', 'lastName', 'email', 'password', 'birthdate', 'alias'])
print_Info(foo)
add a comment |
I thinks you can use this method to get access to the attributes of another Class
class Foo:
def __init__(self, args):
self.personalInfo = args
def print_Info(self):
print self.personalInfo
foo = Foo(['firstName', 'lastName', 'email', 'password', 'birthdate', 'alias'])
print_Info(foo)
add a comment |
I thinks you can use this method to get access to the attributes of another Class
class Foo:
def __init__(self, args):
self.personalInfo = args
def print_Info(self):
print self.personalInfo
foo = Foo(['firstName', 'lastName', 'email', 'password', 'birthdate', 'alias'])
print_Info(foo)
I thinks you can use this method to get access to the attributes of another Class
class Foo:
def __init__(self, args):
self.personalInfo = args
def print_Info(self):
print self.personalInfo
foo = Foo(['firstName', 'lastName', 'email', 'password', 'birthdate', 'alias'])
print_Info(foo)
answered Nov 25 '18 at 3:14
ZhangGaZhangGa
262
262
add a comment |
add a comment |
You don't show the Account class's init method (should be right after the class declaration), but make sure you clearly define the variable name in the arguments. This will ensure it is named correctly and passed to following methods.
def __init__(personalInfo=):
self.personalInfo = personalInfo
..
#then later (or in another script that imports Account):
acct = Account(['John', 'Doe', 'email', 'password', '1/1/99', 'JohnDoe Alias'])
add a comment |
You don't show the Account class's init method (should be right after the class declaration), but make sure you clearly define the variable name in the arguments. This will ensure it is named correctly and passed to following methods.
def __init__(personalInfo=):
self.personalInfo = personalInfo
..
#then later (or in another script that imports Account):
acct = Account(['John', 'Doe', 'email', 'password', '1/1/99', 'JohnDoe Alias'])
add a comment |
You don't show the Account class's init method (should be right after the class declaration), but make sure you clearly define the variable name in the arguments. This will ensure it is named correctly and passed to following methods.
def __init__(personalInfo=):
self.personalInfo = personalInfo
..
#then later (or in another script that imports Account):
acct = Account(['John', 'Doe', 'email', 'password', '1/1/99', 'JohnDoe Alias'])
You don't show the Account class's init method (should be right after the class declaration), but make sure you clearly define the variable name in the arguments. This will ensure it is named correctly and passed to following methods.
def __init__(personalInfo=):
self.personalInfo = personalInfo
..
#then later (or in another script that imports Account):
acct = Account(['John', 'Doe', 'email', 'password', '1/1/99', 'JohnDoe Alias'])
answered Nov 25 '18 at 3:15
AlecZAlecZ
17516
17516
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53464280%2fpython-accessing-list-elements-from-other-class-methods%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
if
personalInfois defined within theAccountclass, then you could just define it asself.personalInfo = ['firstName', 'lastName', 'email', 'password', 'birthdate', 'alias'].– John Anderson
Nov 25 '18 at 3:01
3
There seems to be some context missing. What is the for loop even for? You never use
i.– gilch
Nov 25 '18 at 3:03
@JohnAnderson I tried that but it didn't seem to work. I could have forgotten within editing thought, so I'll give it a try again.
– H4MMY
Nov 25 '18 at 3:04
@gilch Yeah, I tried to keep it concise as well as I thought it would be enough to answer. I can edit the rest in if I was wrong. And as far as the
iin myforloop, it's the only way I've been taught to represent "each item" in the list.– H4MMY
Nov 25 '18 at 3:07
What we really want is an mvce. Try to reduce your code to illustrate the problem only, but completely.
– gilch
Nov 25 '18 at 3:10