我正在嘗試編寫一個程序,該程序從另一個名為 Student.txt 的 python 文件中調用已建立的類。在文件 Student 中,建立了一個名為 StudentInfo 的類,并且init檢查數據是否有效(例如,成績必須在 9-12 之間,課程代碼必須符合格式等)。我嘗試首先在此處獲取用戶的輸入。import studentimport transcriptdef add_student(data): dataDict = data ID = str(len(dataDict) + 1) student = StudentInfo(ID, input("Enter the student\'s last name: "), input("Enter the student\'s first name: "), input("Enter the student\'s grade: "), transcript.add_transcript(), input("Is the student registered: ")) return dataDict當我嘗試將學生定義為 StudentInfo 類的對象時,它返回 NameError: name 'StudentInfo' is not defined.我不確定我做錯了什么。我認為這可能是輸入,但當我刪除它們時,它似乎做了同樣的事情。請提前提供幫助并致謝。
2 回答

達令說
TA貢獻1821條經驗 獲得超6個贊
student.StudentInfo
如果您使用的是import student
.
或者,您可以導入為:
from student import StudentInfo
使用您現在擁有的代碼。

蝴蝶不菲
TA貢獻1810條經驗 獲得超4個贊
看來您忘記了StudentInfo
前綴student
。您可以替換:
import student
和:
from student import StudentInfo
或者您可以替換:
student = StudentInfo(ID, input("Enter the student\'s last name: "), input("Enter the student\'s first name: "), input("Enter the student\'s grade: "), transcript.add_transcript(), input("Is the student registered: "))
和:
student = student.StudentInfo(ID, input("Enter the student\'s last name: "), input("Enter the student\'s first name: "), input("Enter the student\'s grade: "), transcript.add_transcript(), input("Is the student registered: "))
旁注:您不應該在導入后命名變量。將變量重命名student
為其他名稱。
- 2 回答
- 0 關注
- 200 瀏覽
添加回答
舉報
0/150
提交
取消