这道题我选择用来if语句。但是其中出现了一个问题。
当我判断未来天数的时候。我让今天加上未来的天数除余的方式来判断。可系统提示我future_eng不存在
if和elif的使用区别
today_num = eval(input("Enter today's day: ")) elapsed_num = eval(input("Enter the number of days elapsed since today: ")) if today_num == 0: today_eng = "Sunday" elif today_num == 1: today_eng = "Monday" elif today_num == 2: today_eng = "Tuesday" elif today_num == 3: today_eng = "Wednesday" elif today_num == 4: today_eng = "Thursday" elif today_num == 5: today_eng = "Friday" elif today_num == 6: today_eng = "Saderday"在此处请大家注意,这个是我第一个要提醒大家的 就是if和elif 的区别 下面这个if 我之前是用elif的,这看似方便,但是造成了系统运行过程中出现了多次的误判。 所以第一条建议 一个程序里两个不同的if条件请分开来设计。不要包含在同一个if语句下不断elif。
if (today_num + elapsed_num) % 7 == 0: future_eng = "Sunday" elif (today_num + elapsed_num) % 7 == 1: future_eng = "Monday" elif (today_num + elapsed_num) % 7 == 2: future_eng = "Tuesday" elif (today_num + elapsed_num) % 7 == 3: future_eng = "Wednesday" elif (today_num + elapsed_num) % 7 == 4: future_eng = "Thursday" elif (today_num + elapsed_num) % 7 == 5: future_eng = "Friday" elif (today_num + elapsed_num) % 7 == 6: future_eng = "Saderday" print(f"Today is {today_eng} and the future day is {future_eng}")谢谢。这是2021-11-30早上写的。 截稿时间2021-11-30 9:07
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)