Python 如何更改日历列表中的值?
我的函数的目标是在该日历中预订一个与参数day和time相对应的时段-通过将日历中的值更改为“X”(表示已预订日期/时间)来完成预订。以下是我目前掌握的代码:Python 如何更改日历列表中的值?,python,string,list,function,calendar,Python,String,List,Function,Calendar,我的函数的目标是在该日历中预订一个与参数day和time相对应的时段-通过将日历中的值更改为“X”(表示已预订日期/时间)来完成预订。以下是我目前掌握的代码: def find_index(val, seq): for index in range(len(seq)): place = seq[index] if place == val: return index else: return
def find_index(val, seq):
for index in range(len(seq)):
place = seq[index]
if place == val:
return index
else:
return int("-1")
def init_nested_list(size_outer, size_inner):
cal = []
for outer_index in range(size_outer):
nested_list = []
for inner_index in range(size_inner):
nested_list.append("-")
cal.append(nested_list)
return cal
def book_slot(cal,days_labels, times_labels, day, time):
pos1 = find_index(day, days_labels)
desired_day = cal[pos1]
pos2 = find_index(time, times_labels)
desired_time = desired_day[pos2]
if desired_day == "X":
print("We are sorry - that time is not available. Please try again.")
else:
print("Appointment is booked.")
days_labels = ["Wednesday","Thursday","Friday"]
times_labels = ["9","10","11"]
cal = init_nested_list(len(days_labels), len(times_labels))
print("before" , cal)
book_slot(cal, days_labels, times_labels, "Friday", "10")
print("after" , cal)
这是我得到的输出:
before [['-', '-', '-'], ['-', '-', '-'], ['-', '-', '-']]
Appointment is booked.
after [['-', '-', '-'], ['-', '-', '-'], ['-', '-', '-']]
这是我应该得到的输出:
before [['-', '-', '-'], ['-', '-', '-'], ['-', '-', '-']]
Appointment is booked.
after [['-', '-', '-'], ['-', '-', '-'], ['-', 'X', '-']]
We are sorry - that time is not available. Please try again.
如您所见,正确的输出将记录嵌套列表中的第二个元素(星期五,第10页),但对于我的代码来说,它不是。我知道desired_day是用户希望预订的cal中的一天,但我不确定如何正确获取它,并在用户预订时将其分配给字符串“X”。我也知道book_slot函数中有一个编码错误导致了这种情况,但我再一次不确定它是什么…请帮助?在
book_slot中没有任何地方
您是否将X
分配给任何东西。我认为您可以使用pos1
和pos2
来确定要分配到的插槽。这将起作用。请参阅代码中的更改#注释
def find_index(val, seq):
return seq.index(val) #Change 1
def init_nested_list(size_outer, size_inner):
cal = []
for outer_index in range(size_outer):
nested_list = []
for inner_index in range(size_inner):
nested_list.append("-")
cal.append(nested_list)
return cal
def book_slot(cal, days_labels, times_labels, day, time):
pos1 = find_index(day, days_labels)
desired_day = cal[pos1]
pos2 = find_index(time, times_labels)
if desired_day[pos2] is "X": # Change 2
print("We are sorry - that time is not available. Please try again.")
else:
print("Appointment is booked.")
desired_day[pos2] = "X" # Change 3
days_labels = ["Wednesday", "Thursday", "Friday"]
times_labels = ["9", "10", "11"]
cal = init_nested_list(len(days_labels), len(times_labels))
print("before", cal)
book_slot(cal, days_labels, times_labels, "Friday", "10")
book_slot(cal, days_labels, times_labels, "Friday", "10") # Change 4: Need to do a booking twice to make it fail :-)
print("after", cal)
将输出:
('before', [['-', '-', '-'], ['-', '-', '-'], ['-', '-', '-']])
Appointment is booked.
('after', [['-', '-', '-'], ['-', '-', '-'], ['-', 'X', '-']])
We are sorry - that time is not available. Please try again.