【问题标题】:Initiating a new object of class using user input使用用户输入启动一个新的类对象
【发布时间】:2020-03-26 23:17:28
【问题描述】:

我正在尝试获取用户输入以输入新学生的详细信息,然后使用新的详细信息创建一个新的类对象,并调用 print_details 方法来显示新学生的详细信息。有人能指出我正确的方向吗?

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()

【问题讨论】:

    标签: python python-3.x class oop input


    【解决方案1】:

    您可以为此使用input

    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
    

    实现循环(参见 cmets):

    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
    

    【讨论】:

    • @Chikiller 我已更新我的答案以包含此解决方案。下次请在最初的问题帖子中包含您的整个问题。
    【解决方案2】:
    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()
    

    这可能会有所帮助:)

    【讨论】:

      【解决方案3】:
      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()
         
      

      使用列表推导清理了代码。 实际代码@VJAYSLN

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-09-16
        • 2015-11-11
        • 2020-03-05
        • 1970-01-01
        • 1970-01-01
        • 2023-03-05
        • 1970-01-01
        • 2020-07-18
        相关资源
        最近更新 更多