How to use For loop to print all items in python class
I have created a new class:
testing.py:
class student:
def __init__(self, name, major, gpa):
self.nm = name
self.mj = major
self.gp = gpa
then, I move to my working file testing.py:
from testing import student
student1=student("Marcos","Physics",3.99)
student2=student("Phillyp","Biology",2.99)
student3=student("Naim", "Architecture", 3.42)
for k in range(1,4):
print(student(k).gp) <------- how do I merge this string with int!?!?
my final aim is to print out ALL the gpa of all students, so I know I need to do
print(student1.gp)
print(student2.gp)
print(student3.gp)
So how can I concatenate the k into the variable name to produce student1.gp, student2.gp etc?
Thank you all so much!
python loops class for-loop
add a comment |
I have created a new class:
testing.py:
class student:
def __init__(self, name, major, gpa):
self.nm = name
self.mj = major
self.gp = gpa
then, I move to my working file testing.py:
from testing import student
student1=student("Marcos","Physics",3.99)
student2=student("Phillyp","Biology",2.99)
student3=student("Naim", "Architecture", 3.42)
for k in range(1,4):
print(student(k).gp) <------- how do I merge this string with int!?!?
my final aim is to print out ALL the gpa of all students, so I know I need to do
print(student1.gp)
print(student2.gp)
print(student3.gp)
So how can I concatenate the k into the variable name to produce student1.gp, student2.gp etc?
Thank you all so much!
python loops class for-loop
what do you mean exactly by fixing the loop?
– Alexander
Nov 26 '18 at 1:06
add a comment |
I have created a new class:
testing.py:
class student:
def __init__(self, name, major, gpa):
self.nm = name
self.mj = major
self.gp = gpa
then, I move to my working file testing.py:
from testing import student
student1=student("Marcos","Physics",3.99)
student2=student("Phillyp","Biology",2.99)
student3=student("Naim", "Architecture", 3.42)
for k in range(1,4):
print(student(k).gp) <------- how do I merge this string with int!?!?
my final aim is to print out ALL the gpa of all students, so I know I need to do
print(student1.gp)
print(student2.gp)
print(student3.gp)
So how can I concatenate the k into the variable name to produce student1.gp, student2.gp etc?
Thank you all so much!
python loops class for-loop
I have created a new class:
testing.py:
class student:
def __init__(self, name, major, gpa):
self.nm = name
self.mj = major
self.gp = gpa
then, I move to my working file testing.py:
from testing import student
student1=student("Marcos","Physics",3.99)
student2=student("Phillyp","Biology",2.99)
student3=student("Naim", "Architecture", 3.42)
for k in range(1,4):
print(student(k).gp) <------- how do I merge this string with int!?!?
my final aim is to print out ALL the gpa of all students, so I know I need to do
print(student1.gp)
print(student2.gp)
print(student3.gp)
So how can I concatenate the k into the variable name to produce student1.gp, student2.gp etc?
Thank you all so much!
python loops class for-loop
python loops class for-loop
edited Nov 26 '18 at 1:16
MT32
asked Nov 26 '18 at 1:04
MT32MT32
3231213
3231213
what do you mean exactly by fixing the loop?
– Alexander
Nov 26 '18 at 1:06
add a comment |
what do you mean exactly by fixing the loop?
– Alexander
Nov 26 '18 at 1:06
what do you mean exactly by fixing the loop?
– Alexander
Nov 26 '18 at 1:06
what do you mean exactly by fixing the loop?
– Alexander
Nov 26 '18 at 1:06
add a comment |
2 Answers
2
active
oldest
votes
instead of this for k in range(1,4): you want to iterate over a list of all of your students:
students = [student1, student2, student3]
for student in students:
print(student.gp)
EDIT
If you want to be able to reference the students by name, store them in a dict:
students = {'student1': student("Marcos","Physics",3.99),
'student2': student("Phillyp","Biology",2.99),
'student3': student("Naim", "Architecture", 3.42)}
for i in range(1, 4):
print(students[f'student{i}'].gp)
# if less than python 3.6
# print(students['student{}'.format(i)].gp)
thanks for the suggestion, but my ultimate aim is to know how to concatenate that k (integer) into "student" (is this considered a string?)
– MT32
Nov 26 '18 at 1:10
@MT32 No it's not a string, it's a variable name. I would highly recommend you use a list to store your students.
– slider
Nov 26 '18 at 1:14
@MT32 see my edit
– SuperShoot
Nov 26 '18 at 1:15
add a comment |
What you should be doing is putting all the objects in list. E.g.
from testing import student
student1 = student("Marcos","Physics",3.99)
student2 = student("Phillyp","Biology",2.99)
student3 = student("Naim", "Architecture", 3.42)
students = [student1, student2, student3]
for student in students:
print(student.gp)
thanks for the suggestion sir, but that is what I am currently doing. But Im actually looking for suggestions to concatenate the k in for loop into the variable name....
– MT32
Nov 26 '18 at 1:24
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%2f53473575%2fhow-to-use-for-loop-to-print-all-items-in-python-class%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
instead of this for k in range(1,4): you want to iterate over a list of all of your students:
students = [student1, student2, student3]
for student in students:
print(student.gp)
EDIT
If you want to be able to reference the students by name, store them in a dict:
students = {'student1': student("Marcos","Physics",3.99),
'student2': student("Phillyp","Biology",2.99),
'student3': student("Naim", "Architecture", 3.42)}
for i in range(1, 4):
print(students[f'student{i}'].gp)
# if less than python 3.6
# print(students['student{}'.format(i)].gp)
thanks for the suggestion, but my ultimate aim is to know how to concatenate that k (integer) into "student" (is this considered a string?)
– MT32
Nov 26 '18 at 1:10
@MT32 No it's not a string, it's a variable name. I would highly recommend you use a list to store your students.
– slider
Nov 26 '18 at 1:14
@MT32 see my edit
– SuperShoot
Nov 26 '18 at 1:15
add a comment |
instead of this for k in range(1,4): you want to iterate over a list of all of your students:
students = [student1, student2, student3]
for student in students:
print(student.gp)
EDIT
If you want to be able to reference the students by name, store them in a dict:
students = {'student1': student("Marcos","Physics",3.99),
'student2': student("Phillyp","Biology",2.99),
'student3': student("Naim", "Architecture", 3.42)}
for i in range(1, 4):
print(students[f'student{i}'].gp)
# if less than python 3.6
# print(students['student{}'.format(i)].gp)
thanks for the suggestion, but my ultimate aim is to know how to concatenate that k (integer) into "student" (is this considered a string?)
– MT32
Nov 26 '18 at 1:10
@MT32 No it's not a string, it's a variable name. I would highly recommend you use a list to store your students.
– slider
Nov 26 '18 at 1:14
@MT32 see my edit
– SuperShoot
Nov 26 '18 at 1:15
add a comment |
instead of this for k in range(1,4): you want to iterate over a list of all of your students:
students = [student1, student2, student3]
for student in students:
print(student.gp)
EDIT
If you want to be able to reference the students by name, store them in a dict:
students = {'student1': student("Marcos","Physics",3.99),
'student2': student("Phillyp","Biology",2.99),
'student3': student("Naim", "Architecture", 3.42)}
for i in range(1, 4):
print(students[f'student{i}'].gp)
# if less than python 3.6
# print(students['student{}'.format(i)].gp)
instead of this for k in range(1,4): you want to iterate over a list of all of your students:
students = [student1, student2, student3]
for student in students:
print(student.gp)
EDIT
If you want to be able to reference the students by name, store them in a dict:
students = {'student1': student("Marcos","Physics",3.99),
'student2': student("Phillyp","Biology",2.99),
'student3': student("Naim", "Architecture", 3.42)}
for i in range(1, 4):
print(students[f'student{i}'].gp)
# if less than python 3.6
# print(students['student{}'.format(i)].gp)
edited Nov 26 '18 at 1:21
answered Nov 26 '18 at 1:07
SuperShootSuperShoot
1,808719
1,808719
thanks for the suggestion, but my ultimate aim is to know how to concatenate that k (integer) into "student" (is this considered a string?)
– MT32
Nov 26 '18 at 1:10
@MT32 No it's not a string, it's a variable name. I would highly recommend you use a list to store your students.
– slider
Nov 26 '18 at 1:14
@MT32 see my edit
– SuperShoot
Nov 26 '18 at 1:15
add a comment |
thanks for the suggestion, but my ultimate aim is to know how to concatenate that k (integer) into "student" (is this considered a string?)
– MT32
Nov 26 '18 at 1:10
@MT32 No it's not a string, it's a variable name. I would highly recommend you use a list to store your students.
– slider
Nov 26 '18 at 1:14
@MT32 see my edit
– SuperShoot
Nov 26 '18 at 1:15
thanks for the suggestion, but my ultimate aim is to know how to concatenate that k (integer) into "student" (is this considered a string?)
– MT32
Nov 26 '18 at 1:10
thanks for the suggestion, but my ultimate aim is to know how to concatenate that k (integer) into "student" (is this considered a string?)
– MT32
Nov 26 '18 at 1:10
@MT32 No it's not a string, it's a variable name. I would highly recommend you use a list to store your students.
– slider
Nov 26 '18 at 1:14
@MT32 No it's not a string, it's a variable name. I would highly recommend you use a list to store your students.
– slider
Nov 26 '18 at 1:14
@MT32 see my edit
– SuperShoot
Nov 26 '18 at 1:15
@MT32 see my edit
– SuperShoot
Nov 26 '18 at 1:15
add a comment |
What you should be doing is putting all the objects in list. E.g.
from testing import student
student1 = student("Marcos","Physics",3.99)
student2 = student("Phillyp","Biology",2.99)
student3 = student("Naim", "Architecture", 3.42)
students = [student1, student2, student3]
for student in students:
print(student.gp)
thanks for the suggestion sir, but that is what I am currently doing. But Im actually looking for suggestions to concatenate the k in for loop into the variable name....
– MT32
Nov 26 '18 at 1:24
add a comment |
What you should be doing is putting all the objects in list. E.g.
from testing import student
student1 = student("Marcos","Physics",3.99)
student2 = student("Phillyp","Biology",2.99)
student3 = student("Naim", "Architecture", 3.42)
students = [student1, student2, student3]
for student in students:
print(student.gp)
thanks for the suggestion sir, but that is what I am currently doing. But Im actually looking for suggestions to concatenate the k in for loop into the variable name....
– MT32
Nov 26 '18 at 1:24
add a comment |
What you should be doing is putting all the objects in list. E.g.
from testing import student
student1 = student("Marcos","Physics",3.99)
student2 = student("Phillyp","Biology",2.99)
student3 = student("Naim", "Architecture", 3.42)
students = [student1, student2, student3]
for student in students:
print(student.gp)
What you should be doing is putting all the objects in list. E.g.
from testing import student
student1 = student("Marcos","Physics",3.99)
student2 = student("Phillyp","Biology",2.99)
student3 = student("Naim", "Architecture", 3.42)
students = [student1, student2, student3]
for student in students:
print(student.gp)
answered Nov 26 '18 at 1:20
BatmanBatman
4,54031551
4,54031551
thanks for the suggestion sir, but that is what I am currently doing. But Im actually looking for suggestions to concatenate the k in for loop into the variable name....
– MT32
Nov 26 '18 at 1:24
add a comment |
thanks for the suggestion sir, but that is what I am currently doing. But Im actually looking for suggestions to concatenate the k in for loop into the variable name....
– MT32
Nov 26 '18 at 1:24
thanks for the suggestion sir, but that is what I am currently doing. But Im actually looking for suggestions to concatenate the k in for loop into the variable name....
– MT32
Nov 26 '18 at 1:24
thanks for the suggestion sir, but that is what I am currently doing. But Im actually looking for suggestions to concatenate the k in for loop into the variable name....
– MT32
Nov 26 '18 at 1:24
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%2f53473575%2fhow-to-use-for-loop-to-print-all-items-in-python-class%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
what do you mean exactly by fixing the loop?
– Alexander
Nov 26 '18 at 1:06