Python 使用用户输入初始化类的新对象
我正在尝试获取用户输入以输入新学生的详细信息,然后使用新的详细信息创建一个新的类对象,并调用print_details方法来显示新学生的详细信息。有人能给我指出正确的方向吗Python 使用用户输入初始化类的新对象,python,python-3.x,class,oop,input,Python,Python 3.x,Class,Oop,Input,我正在尝试获取用户输入以输入新学生的详细信息,然后使用新的详细信息创建一个新的类对象,并调用print_details方法来显示新学生的详细信息。有人能给我指出正确的方向吗 class Student: def __init__(self, name, age, course, ID): self.name = name self.age = age self.course = course self.ID = ID
class Student:
def __init__(self, name, age, course, ID):
self.name = name
self.age = age
self.course = course
self.ID = ID
def print_details(self):
print("Name: " + self.name)
print("Age: " + str(self.age))
print("Course: " + self.course)
print("Student ID: " + self.ID)
student1 = Student("Bob", 20, "Computer Science","1000121")
student2 = Student("Alice", 21, "Computer Science", "1000475")
student3 = Student("Jane", 18, "Information Technology", "1000823")
student1.print_details()
student2.print_details()
student3.print_details()
您可以仅使用以下功能:
student4 = Student(input("Name:"), int(input("Age:")), input("Subject:"), input("ID:"))
student4.print_details()
输出:
>>> Name:Bob
>>> Age:16
>>> Subject:Maths
>>> ID:1234123
Name: Bob
Age: 16
Course: Maths
Student ID: 1234123
students = []
while True:
if input("Type stop to stop, otherwise hit enter.") == "stop":
break
students.append(Student(input("Name:"), input("Age:"), input("Subject:"), input("ID:")))
for student in students:
student.print_details()
>>> Type stop to stop, otherwise hit enter.
>>> Name:John
>>> Age:15
>>> Subject:IT
>>> ID:3456789
>>> Type stop to stop, otherwise hit enter.
>>> Name:Mary
>>> Age:76
>>> Subject:Gardening
>>> ID:4567890
>>> Type stop to stop, otherwise hit enter.stop
Name: John
Age: 15
Course: IT
Student ID: 3456789
Name: Mary
Age: 76
Course: Gardening
Student ID: 4567890
要实现循环(请参见注释):
>>> Name:Bob
>>> Age:16
>>> Subject:Maths
>>> ID:1234123
Name: Bob
Age: 16
Course: Maths
Student ID: 1234123
students = []
while True:
if input("Type stop to stop, otherwise hit enter.") == "stop":
break
students.append(Student(input("Name:"), input("Age:"), input("Subject:"), input("ID:")))
for student in students:
student.print_details()
>>> Type stop to stop, otherwise hit enter.
>>> Name:John
>>> Age:15
>>> Subject:IT
>>> ID:3456789
>>> Type stop to stop, otherwise hit enter.
>>> Name:Mary
>>> Age:76
>>> Subject:Gardening
>>> ID:4567890
>>> Type stop to stop, otherwise hit enter.stop
Name: John
Age: 15
Course: IT
Student ID: 3456789
Name: Mary
Age: 76
Course: Gardening
Student ID: 4567890
输出:
>>> Name:Bob
>>> Age:16
>>> Subject:Maths
>>> ID:1234123
Name: Bob
Age: 16
Course: Maths
Student ID: 1234123
students = []
while True:
if input("Type stop to stop, otherwise hit enter.") == "stop":
break
students.append(Student(input("Name:"), input("Age:"), input("Subject:"), input("ID:")))
for student in students:
student.print_details()
>>> Type stop to stop, otherwise hit enter.
>>> Name:John
>>> Age:15
>>> Subject:IT
>>> ID:3456789
>>> Type stop to stop, otherwise hit enter.
>>> Name:Mary
>>> Age:76
>>> Subject:Gardening
>>> ID:4567890
>>> Type stop to stop, otherwise hit enter.stop
Name: John
Age: 15
Course: IT
Student ID: 3456789
Name: Mary
Age: 76
Course: Gardening
Student ID: 4567890
这可能会有帮助:)
使用列表理解清理代码。
实际代码@VJAYSLN@Chikiller我已经更新了答案,将此解决方案包括在内。下次请把你的全部问题写在第一个问题帖里。