我的目標是:我需要讓用戶輸入電子郵件的數量,然後啓動一個for循環來註冊輸入的電子郵件。然後,這些電子郵件將根據'@ professor.com'和'@ student.com'分類。這將在列表中作爲追加計數。以下是我已經嘗試過從Python中輸入的值中提取電子郵件
email_count = int(input('how many emails you want'))
student_email_count = 0
professor_email_count = 0
student_email = '@student.com'
professor_email = '@professor.com'
for i in range(email_count):
email_list = str(input('Enter the emails')
if email_list.index(student_email):
student_email_count = student_email_count + 1
elif email_list.index(professor_email):
professor_email_count = professor_email_count + 1
有人可以幫助縮短這個,並寫下專業解釋以供進一步參考?在這裏,追加部分丟失了。那裏有人能通過一些光線嗎?
謝謝
如果你創建一個列表,然後使用它的'.append'方法將做追加...你嘗試運行你的代碼,雖然並輸入混合電子郵件 - 你已經得到另一個錯誤g Oing to crop up ... –
電子郵件是一次輸入一個嗎?或多個電子郵件分隔什麼,空間? – Vinny
嗨,喬恩。感謝您的反饋。我注意到只找到'@professor'索引。但是,「@學生」索引不起作用。它給出了一個錯誤,指出找不到值error-substring。這是爲什麼。如果可能的話,可否請您重寫代碼,可能會更有效率 – jeff