因此,我开始做一个项目,比较两个人的繁忙时间,并输出一个列表,其中有两个人的可用时间来设置约会 输入:
Person 1 = [["9:00", "10:30"], ["12:00", "13:00"], ["16:00", "18:00"]]
Person 2 = [["10:00", "11:30"], ["12:30", "14:30"], ["14:30", "15:00"], ['16:00', '17:00']]
输出:
list = [["11:30","1200"],["15:00","16:00"]]
def Gettime(time1, time2):
hour1, minutes1 = time1.split(":")
hour2, minutes2 = time2.split(":")
int_time1 = int(hour1) * 60 + int(minutes1)
int_time2 = int(hour2) * 60 + int(minutes2)
if int_time1 <= int_time2:
return 1
elif int_time1 > int_time2:
return 2
else:
return 0
def FreeTime(sch1, sch2):
list_nottime = []
p1 = 0
p2 = 0
x = 0
y = 0
#while p2 < len(sch2) :
#while p1 < len(sch1):
while p1 < len(sch1) or p2 <len(sch2):
if Gettime(sch1[p1][x], sch2[p2][y]) == 1:
#print(sch1[p1][x], sch2[p2][y])
list_nottime.append(sch1[p1][x])
x += 1
if x == 2:
x = 0
p1 += 1
if Gettime(sch1[p1][x], sch2[p2][y]) == 2:
#print(sch1[p1][x], sch2[p2][y])
list_nottime.append(sch2[p2][y])
y += 1
if y == 2:
y = 0
p2 += 1
因此在这一端(正上方)p2 == 4
,我们的while条件是p2 < len(Sch2)
(等于4)
它仍然运行循环,然后出错。
请检查并修复它,让你们大吃一惊!!!!(我不是以英语为母语的人,所以我的英语不好)
因此,问题是在调用第二个while循环之前,您正在增加
p1
的值,第二个while循环中的条件访问sch[p1]
,这可能超出给定数组的界限。您需要将p1保存在临时变量中,并在更新p1
的值之前使用该值或者,您需要一个条件来检查边或边界情况。p2也是如此,它在第二个内部循环中递增,第一个内部循环可能试图在索引超出范围的情况下访问它
相关问题 更多 >
编程相关推荐