我正试着用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 13:56:02
所以我相信这就是问题所在。错误键错误意味着您向字典传递的键不是字典中的实际键。所以对于一天来说,不是传入例如4,而是传入“星期四”
编辑:例如,字典a:
a = {“Monday” : [“Math”, “Writing”]
#if I wanted to get the value of Monday and get the first
#subject the code would look like this
print(a[“Monday”][0])因此,如果我要写这段代码,保留日期部分。我会这么做的
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"]}
days = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday"]
#Assuming that when you pass in the value day, day is not an exact index
#value, meaning that for example if you wanted Monday, day would be 1
def findClass(weekLetter, day, period):
if weekLetter == "A" or weekLetter == "a":
return {weeka[days[day - 1]][period]}
elif weekLetter == "B" or weekLetter == "b":
return {weekb[days[day - 1]][period]}
else:
return "Invalid input. Please try again."发布于 2021-02-03 14:18:33
当您调用函数findClass时,您的代码似乎可以正常工作,并将weekLetter和fine.Just作为字符串传递。
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."
findClass('A','Wednesday',2)发布于 2021-02-03 14:18:51
这会抛出一个错误,因为这里的weeka[day][period]是您试图访问不存在的句号(嵌套字典)。要解决这个问题,我建议您修改weeka和weekb,如下所示
weeka = {
'Monday': {
'period_a': ["Legal", "Religion", "English", "Maths", "Study"],
'period_b': ["Legal", "Religion", "English", "Maths", "Study"]
},
'Tuesday': {
'period_a': ["Legal", "Religion", "English", "Maths", "Study"],
'period_b': ["Legal", "Religion", "English", "Maths", "Study"]
},
'Wednesday': {
'period_a': ["Legal", "Religion", "English", "Maths", "Study"],
'period_b': ["Legal", "Religion", "English", "Maths", "Study"]
},
'Thursday': {
'period_a': ["Legal", "Religion", "English", "Maths", "Study"],
'period_b': ["Legal", "Religion", "English", "Maths", "Study"]
},
'Friday': {
'period_a': ["Legal", "Religion", "English", "Maths", "Study"],
'period_b': ["Legal", "Religion", "English", "Maths", "Study"]
},
}
def findClass(weekLetter, day, period):
if weekLetter.lower() == "a":
return weeka[day][period]
elif weekLetter.lower() == "b":
return weekb[day][period]
else :
return "Invalid input. Please try again."
period = findClass('A', 'Monday', 'period_a')
print(period)
请注意,我已经将
if weekLetter == "A" or weekLetter == "a":更改为weekLetter.lower() == "a":,这样您就不必检查相同的条件两次
https://stackoverflow.com/questions/66021948
复制相似问题