簡體   English   中英

創建學生 class 並從用戶輸入打印

[英]Creating a Student class and printing from user input

我們被要求做以下事情:

使用公共成員變量創建 class 學生:學生姓名、學號、聯系電話、身份證號。 需要以下規格:

  • 增加 class 的 init() 方法,將字符串成員變量初始化為空字符串,將數值初始化為 0。
  • 將方法 populate() 添加到 class。 該方法用於為 class 的成員變量賦值。
  • 將方法 display() 添加到 class。 該方法用於顯示class的成員變量。 (2)
  • 在主程序中創建 class Student 的實例 StudentObj。
  • 主程序應提示用戶輸入五個學生的值。 屬性應使用其 populate() 方法分配給 Student 實例,並且必須使用 Student 實例的 display() 方法顯示。

I have created an int function which initialises string variables to empty strings and numeric values to int variables, a populate function which assigns values to variables in the class and a display method to print the values of the class. main 方法應該要求用戶輸入他們的詳細信息,然后使用 display() 方法打印這些詳細信息。

class Student:
    def __init__(self, student_name, student_num,contact_num,ID_num):
        self.student_name = ""
        self.student_num = 0
        contact_num = 0
        ID_num = 0

    def populate(self,student_name, student_num,contact_num,ID_num):
        self.student_name = student_name
        self.student_num = student_num
        contact_num = contact_num
        ID_num = ID_num

    def display(student_name, student_num,contact_num,ID_num):
        print(student_name)
        print(student_num)
        print(contact_num)
        print(ID_num)

def main():
    StudentObj = Student()

    s_name = int(input("Enter student name: "))
    s_num = int(input("Enter student number: "))
    con_num = int(input("Enter contact number: "))
    ID_num = int(input("Enter ID number: "))

    populate(s_name,s_num,con_num,ID_num)
    display(s_name,s_num,con_num,ID_num)
if __name__ == '__main__':
    main()

程序以退出代碼 0 結束,但它不要求任何輸入或打印任何內容

正如@Xteven 建議的那樣,缺少對主要 function 的調用。

你需要

if __name__ == '__main__':
    main()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM