'Create and store objects in list Python
class Prog:
def __init__(self, name, course, languange):
self.name = name
self.course = course
self.languange = languange
def show(self):
print("Name:", self.name, "\nCourse:", self.course, "\nLanguage:", self.languange, "\n")
#Complete code
Students = []
#Create and store the students info (objects) in the list Students
number_of = int(input("Number of students?: "))
#Complete code
loop = 0
while loop < antal:
print("Name, course language?") #here I want the user to type the name, course and what programming language he/she is studying
print("The following students are now in the system")
#Complete code
I want the output to be:
Number of students?: 2
Name, course, language?
Alan
P1
Python
Name, course, language?
Jane
P2
Python
The following students are now in the system:
Name : Alan
Course : P1
Language : Python
Name : Jane
Course : P2
Language : Python
I can't seem to give self.name, self.course, self.language the input() value in the list Students = []
I did try to .appendto the list but when I write p.Prog(Students)I get this error message TypeError: __init__() missing 2 required positional arguments: 'course' and 'languange'
This is the code I wrote to store values into the list.
Students = []
number_of = int(input("Number of students?: "))
loop = 0
while loop < number_of:
print("Name, course, language?")
name = input()
course = input()
language = input()
loop += 1
p = Prog(Students)
print("Following students are now in the system.")
p.show()
Solution 1:[1]
You need to append to Students.
When you call Prog(), you have to provide the 3 values that you just input as parameters. It's not clear why you thought the Students list would be the proper argument there.
Then when you want to list all the students, you have to loop through the Students list.
Students = []
number_of = int(input("Number of students?: "))
for _ in range(number_of):
name = input('Name: ')
course = input('Course: ')
language = input('Language: ')
p = Prog(name, course, language)
Students.append(p)
print("Following students are now in the system.")
for student in Students:
student.show()
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Barmar |
