Could someone take a look at my code and see whats up?
new to coding. Probably obvious. So I want to add 5 names to list and then print the third name from the list. Why isn't this working:
list=
def lol():
return input("Give the name you want to add to the list.")
a=lol()
b=lol()
c=lol()
d=lol()
e=lol()
variables=[a,b,c,d,e]
list.append(variables)
print list[2]
python
add a comment |
new to coding. Probably obvious. So I want to add 5 names to list and then print the third name from the list. Why isn't this working:
list=
def lol():
return input("Give the name you want to add to the list.")
a=lol()
b=lol()
c=lol()
d=lol()
e=lol()
variables=[a,b,c,d,e]
list.append(variables)
print list[2]
python
print list[2]
this question shouldn't be tagged with python-3.x
– TrebuchetMS
Nov 13 '18 at 20:34
1
What's the point oflist
? Why not just dovariables[2]
?
– Carcigenicate
Nov 13 '18 at 20:40
Yeah I could do variables[2], but why is list[2] not working, just trying to learn...
– AksO
Nov 13 '18 at 20:46
Just printlist
andvariables
and you will see what they contain which will explain what your code does.
– barny
Nov 13 '18 at 21:05
add a comment |
new to coding. Probably obvious. So I want to add 5 names to list and then print the third name from the list. Why isn't this working:
list=
def lol():
return input("Give the name you want to add to the list.")
a=lol()
b=lol()
c=lol()
d=lol()
e=lol()
variables=[a,b,c,d,e]
list.append(variables)
print list[2]
python
new to coding. Probably obvious. So I want to add 5 names to list and then print the third name from the list. Why isn't this working:
list=
def lol():
return input("Give the name you want to add to the list.")
a=lol()
b=lol()
c=lol()
d=lol()
e=lol()
variables=[a,b,c,d,e]
list.append(variables)
print list[2]
python
python
edited Nov 13 '18 at 20:42
AksO
asked Nov 13 '18 at 20:30
AksOAksO
1
1
print list[2]
this question shouldn't be tagged with python-3.x
– TrebuchetMS
Nov 13 '18 at 20:34
1
What's the point oflist
? Why not just dovariables[2]
?
– Carcigenicate
Nov 13 '18 at 20:40
Yeah I could do variables[2], but why is list[2] not working, just trying to learn...
– AksO
Nov 13 '18 at 20:46
Just printlist
andvariables
and you will see what they contain which will explain what your code does.
– barny
Nov 13 '18 at 21:05
add a comment |
print list[2]
this question shouldn't be tagged with python-3.x
– TrebuchetMS
Nov 13 '18 at 20:34
1
What's the point oflist
? Why not just dovariables[2]
?
– Carcigenicate
Nov 13 '18 at 20:40
Yeah I could do variables[2], but why is list[2] not working, just trying to learn...
– AksO
Nov 13 '18 at 20:46
Just printlist
andvariables
and you will see what they contain which will explain what your code does.
– barny
Nov 13 '18 at 21:05
print list[2]
this question shouldn't be tagged with python-3.x– TrebuchetMS
Nov 13 '18 at 20:34
print list[2]
this question shouldn't be tagged with python-3.x– TrebuchetMS
Nov 13 '18 at 20:34
1
1
What's the point of
list
? Why not just do variables[2]
?– Carcigenicate
Nov 13 '18 at 20:40
What's the point of
list
? Why not just do variables[2]
?– Carcigenicate
Nov 13 '18 at 20:40
Yeah I could do variables[2], but why is list[2] not working, just trying to learn...
– AksO
Nov 13 '18 at 20:46
Yeah I could do variables[2], but why is list[2] not working, just trying to learn...
– AksO
Nov 13 '18 at 20:46
Just print
list
and variables
and you will see what they contain which will explain what your code does.– barny
Nov 13 '18 at 21:05
Just print
list
and variables
and you will see what they contain which will explain what your code does.– barny
Nov 13 '18 at 21:05
add a comment |
1 Answer
1
active
oldest
votes
Logical issues with your code
You will have a problem accessing an element in third position of the list because there isn't one. When you call append()
the list you are passing will be treated like one element so it will put in first or position 0 all the list.
print list
[[1, 2, 3 , 4, 5]]
Looking at the output, you can see that you have a list of lists and with only one element in position 0, but you are trying to retrieve the one in position 2 and python correctly replies IndexError: list index out of range
.
In case you want that your list to be populated with every element in variables
use extend()
method of this data structure.
Otherwise, first retrieve the list in position 0 and then look directly in it in this way: list[0][2]
.
Note
Try not using reserved keywords like list, dict and so on because they are used for calling some built-in functions of Python language. Your code will be unreadable otherwise.
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%2f53289033%2fcould-someone-take-a-look-at-my-code-and-see-whats-up%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Logical issues with your code
You will have a problem accessing an element in third position of the list because there isn't one. When you call append()
the list you are passing will be treated like one element so it will put in first or position 0 all the list.
print list
[[1, 2, 3 , 4, 5]]
Looking at the output, you can see that you have a list of lists and with only one element in position 0, but you are trying to retrieve the one in position 2 and python correctly replies IndexError: list index out of range
.
In case you want that your list to be populated with every element in variables
use extend()
method of this data structure.
Otherwise, first retrieve the list in position 0 and then look directly in it in this way: list[0][2]
.
Note
Try not using reserved keywords like list, dict and so on because they are used for calling some built-in functions of Python language. Your code will be unreadable otherwise.
add a comment |
Logical issues with your code
You will have a problem accessing an element in third position of the list because there isn't one. When you call append()
the list you are passing will be treated like one element so it will put in first or position 0 all the list.
print list
[[1, 2, 3 , 4, 5]]
Looking at the output, you can see that you have a list of lists and with only one element in position 0, but you are trying to retrieve the one in position 2 and python correctly replies IndexError: list index out of range
.
In case you want that your list to be populated with every element in variables
use extend()
method of this data structure.
Otherwise, first retrieve the list in position 0 and then look directly in it in this way: list[0][2]
.
Note
Try not using reserved keywords like list, dict and so on because they are used for calling some built-in functions of Python language. Your code will be unreadable otherwise.
add a comment |
Logical issues with your code
You will have a problem accessing an element in third position of the list because there isn't one. When you call append()
the list you are passing will be treated like one element so it will put in first or position 0 all the list.
print list
[[1, 2, 3 , 4, 5]]
Looking at the output, you can see that you have a list of lists and with only one element in position 0, but you are trying to retrieve the one in position 2 and python correctly replies IndexError: list index out of range
.
In case you want that your list to be populated with every element in variables
use extend()
method of this data structure.
Otherwise, first retrieve the list in position 0 and then look directly in it in this way: list[0][2]
.
Note
Try not using reserved keywords like list, dict and so on because they are used for calling some built-in functions of Python language. Your code will be unreadable otherwise.
Logical issues with your code
You will have a problem accessing an element in third position of the list because there isn't one. When you call append()
the list you are passing will be treated like one element so it will put in first or position 0 all the list.
print list
[[1, 2, 3 , 4, 5]]
Looking at the output, you can see that you have a list of lists and with only one element in position 0, but you are trying to retrieve the one in position 2 and python correctly replies IndexError: list index out of range
.
In case you want that your list to be populated with every element in variables
use extend()
method of this data structure.
Otherwise, first retrieve the list in position 0 and then look directly in it in this way: list[0][2]
.
Note
Try not using reserved keywords like list, dict and so on because they are used for calling some built-in functions of Python language. Your code will be unreadable otherwise.
edited Nov 13 '18 at 21:08
answered Nov 13 '18 at 20:56
IulianIulian
14218
14218
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%2f53289033%2fcould-someone-take-a-look-at-my-code-and-see-whats-up%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
print list[2]
this question shouldn't be tagged with python-3.x– TrebuchetMS
Nov 13 '18 at 20:34
1
What's the point of
list
? Why not just dovariables[2]
?– Carcigenicate
Nov 13 '18 at 20:40
Yeah I could do variables[2], but why is list[2] not working, just trying to learn...
– AksO
Nov 13 '18 at 20:46
Just print
list
andvariables
and you will see what they contain which will explain what your code does.– barny
Nov 13 '18 at 21:05