[英]Creating a Student class and printing from user input
我們被要求做以下事情:
使用公共成員變量創建 class 學生:學生姓名、學號、聯系電話、身份證號。 需要以下規格:
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.