[英]Initiating a new object of class using user input
I'm trying to get user input to enter details for a new student, then using the new details I want to create a new object of class, and call the print_details method to display the details of the new student.我试图让用户输入输入新学生的详细信息,然后使用新的详细信息创建一个新的类对象,并调用 print_details 方法来显示新学生的详细信息。 Is anyone able to point me in the right direction?
有人能指出我正确的方向吗?
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()
You can just use input for this:您可以为此使用输入:
student4 = Student(input("Name:"), int(input("Age:")), input("Subject:"), input("ID:"))
student4.print_details()
Output:输出:
>>> Name:Bob
>>> Age:16
>>> Subject:Maths
>>> ID:1234123
Name: Bob
Age: 16
Course: Maths
Student ID: 1234123
To implement a loop (see comments):实现一个循环(见评论):
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()
Output:输出:
>>> 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
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)
n = int(input("No of students"))
students = []
for i in range(n):
print("Enter Details for student No:",i+1)
s = Student(input("Enter name:"),int(input("Enter age:")),input("Enter course:"),input("Enter ID:"))
students.append(s)
for i in range(len(students)):
print("student No:",i+1)
students[i].print_details()
This Might Helps : )这可能有帮助:)
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)
n = int(input("No of students:"))
students = []
for i in range(n):
print(f"Enter Details for student No.{i+1}")
s = Student(*[input(f'Enter {info}: ')for info in ["Name", "Age", "Course", "ID"]])
students.append(s)
for i in range(len(students)):
print(f"\nStudent {i+1}")
students[i].print_details()
Cleaned the code by using list comprehension.使用列表理解来清理代码。 Actual code @VJAYSLN
实际代码@VJAYSLN
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.