我正试着用Python写一个程序来存储我的课程表。部分用于实践,部分用于使用。我所有的课程都存储在两个字典中,一个用于A周,另一个用于B周。下面是其中一个字典的示例:
weeka = {
"Monday": ["Legal", "Religion", "English", "Maths", "Study"],
"Tuesday:": ["English", "History", "Legal", "Legal", "Study"],
"Wednesday": ["Study", "Maths", "English", "Religion", "History"],
"Thursday": ["Maths", "Study", "History", "Religion"],
"Friday": ["History", "Religion", "English", "Maths", "Legal"]}
def findClass(weekLetter, day, period):
if weekLetter == "A" or weekLetter == "a":
return {weeka[day][period]}
elif weekLetter == "B" or weekLetter == "b":
return {weekb[day][period]}
else:
return "Invalid input. Please try again."当我运行这段代码时,我得到:
Traceback (most recent call last):
File "myTimetable.py", line 41, in <module>
getClass()
File "myTimetable.py", line 35, in getClass
currentClass = findClass(weekLetter, day, period)
File "myTimetable.py", line 19, in findClass
return {weeka[day][period]}
KeyError: 4请注意,在上面的错误中,我得到了函数中提到的所有参数。我该如何解决这个问题?
发布于 2021-02-03 14:47:42
你的代码有几个挑战。
#1:如果星期几的输入格式与字典中存储的格式不同,则会出现错误
#2:在周四,你只有4节课。如果你执行当前的代码,你会得到一个错误。它不会返回有效消息。
#3:你可以把Week-A和Week-B合并成一个字典。字典可以有key a和key b。然后,每个键都可以有自己的一周词典条目。这样,您就可以用相同的代码引用它。
# 4 :当你搜索一个句号时,你实际上给出了句号#s 1到5。Python将其存储为0到4。所以你必须从句号中减去1才能得到正确的句号。
考虑到所有这些,这里有一个可以解决上述问题的代码。
week_dict = {
"a":{
"Monday": ["Legal", "Religion", "English", "Maths", "Study"],
"Tuesday": ["English", "History", "Legal", "Legal", "Study"],
"Wednesday": ["Study", "Maths", "English", "Religion", "History"],
"Thursday": ["Maths", "Study", "History", "Religion"],
"Friday": ["History", "Religion", "English", "Maths", "Legal"]},
"b":{
"Monday": ["English", "History", "Legal", "Legal", "Study"],
"Tuesday": ["Legal", "Religion", "English", "Maths", "Study"],
"Wednesday": ["Maths", "Study", "History", "Religion"],
"Thursday": ["Study", "Maths", "English", "Religion", "History"],
"Friday": ["History", "Religion", "English", "Maths", "Legal"]}}
def findClass(weekLetter, day_of_week, period):
print (weekLetter, day_of_week, period,end = ' : ')
weekLetter = weekLetter.lower()
day_of_week = day_of_week.capitalize()
if weekLetter in ('a','b') and day_of_week in week_dict['a']:
if period <= len(week_dict[weekLetter][day_of_week]):
return week_dict[weekLetter][day_of_week][period-1]
else:
return "Invalid input. Please try again."
else:
return "Invalid input. Please try again."
print (findClass('A','monday',1))
print (findClass('a','tuesday',2))
print (findClass('a','wednesday',3))
print (findClass('a','thursday',5))
print (findClass('A','friday',4))
print (findClass('B','monday',1))
print (findClass('b','tuesday',2))
print (findClass('b','wednesday',3))
print (findClass('B','thursday',5))
print (findClass('b','friday',4))
print (findClass('a','saturday',2))
print (findClass('b','sunday',3))以下内容的输出为:
A monday 1 : Legal
a tuesday 2 : History
a wednesday 3 : English
a thursday 5 : Invalid input. Please try again.
A friday 4 : Maths
B monday 1 : English
b tuesday 2 : Religion
b wednesday 3 : History
B thursday 5 : History
b friday 4 : Maths
a saturday 2 : Invalid input. Please try again.
b sunday 3 : Invalid input. Please try again.注:我已经申请了A周,星期四,第五期。没有。它会返回一个无效的条目。
同样,我要求在周末的A周和B周。它还会返回一个无效的条目。
https://stackoverflow.com/questions/66021948
复制相似问题