以下是我的代碼,可幫助我的學生在忘記密碼時登錄他們的帳戶。今年的校歷是 2019-2020 學年。如果我是五年級學生,我會在 2019 年進入學校。他們用戶名的末尾使用他們高中畢業的年份。因此,我的代碼是從當年開始加上 8 年來計算該數字。然而這是問題所在,因為一個學年被新的一年分開,如果我的五年級學生輸入 2020 年,他們將獲得大 1 個整數的一年。那么我將如何在我的代碼中指定如果在一月到八月之間你需要從一年中減去 1,然后到 2020 年 9 月,新的五年級班級將使用 2020 作為他們的基礎,因此他們的用戶名中將有正確的數字.import datetimeimport sysdef find_info(): first_name = input("What is your first name? ") sys.stdout.write("\033[F") sys.stdout.write("\033[K") last_name = input("What is your last name? ") sys.stdout.write("\033[F") sys.stdout.write("\033[K") grade = input("What grade are you in? ") sys.stdout.write("\033[F") sys.stdout.write("\033[K") year = datetime.date.today().year #month = datetime.date.today().month sys.stdout.write("\033[F") sys.stdout.write("\033[K") #This will only work in September of that year #If it passes January 1st of the school year the end number in username will be invalid #Need to come up with a way for a range of dates using if statement to ensure correct year pin = input("What is your lunch pin / ID number? ") sys.stdout.write("\033[F") sys.stdout.write("\033[K") #if month < : # year -1 grad_year = -1 if grade == "5" or grade == str("5th") or grade == str("5TH") or grade == str("5th Grade") or grade == str("5th grade") or grade == str("5th GRADE") or grade == str("5TH GRADE"): grad_year = year + 8 elif grade == "6" or grade == str("6th") or grade == str("6TH"): grad_year = year + 7 elif grade == "7" or grade == str("7th") or grade == str("7TH"): grad_year = year + 6 else: grad_year = year + 5 print("Hello there " + first_name+"!") print("\n") print("Your username is: " + last_name.lower() + first_name[0].lower() + str(grad_year)[2]+ str(grad_year)[3]) print("Your password is: " + first_name[0].lower() + last_name[0].lower() + pin + "hoh") print("Your Email Address is: " + last_name.lower() + first_name[0].lower() + str(grad_year)[2]+ str(grad_year)[3] + "@learn.hohschools.org")find_info()
1 回答

慕妹3242003
TA貢獻1824條經驗 獲得超6個贊
datetime.date.today().month返回一個從 1 到 12 的整數,因此您可以檢查該month值是否小于或等于 8 以檢查月份是否在 1 月和 8 月之間。
grad_year = 0
if month <= 8:
grad_year-= 1
if grade == "5" or grade == str("5th") or grade == str("5TH"):
grad_year = year + 8
...
添加回答
舉報
0/150
提交
取消