Python 当值通过条件时,为什么我的while循环仍然运行
因此,我开始做一个项目,比较两个人的繁忙时间,并输出一个列表,其中有两个人的可用时间来设置约会 输入: 输出:Python 当值通过条件时,为什么我的while循环仍然运行,python,while-loop,Python,While Loop,因此,我开始做一个项目,比较两个人的繁忙时间,并输出一个列表,其中有两个人的可用时间来设置约会 输入: 输出: list = [["11:30","1200"],["15:00","16:00"]] def Gettime(time1,time2): 小时1,分钟1=时间1.拆分(“:”) 小时2,分钟2=时间2.拆分(“:”) 整数时间1=整数(小时1)*60+整数(分钟1) int_time2=int(小时2)*60+int(分钟2) 如果int_time1 int_time2: 返回2
list = [["11:30","1200"],["15:00","16:00"]]
def Gettime(time1,time2):
小时1,分钟1=时间1.拆分(“:”)
小时2,分钟2=时间2.拆分(“:”)
整数时间1=整数(小时1)*60+整数(分钟1)
int_time2=int(小时2)*60+int(分钟2)
如果int_time1 int_time2:
返回2
其他:
返回0
def空闲时间(sch1、sch2):
列表\u nottime=[]
p1=0
p2=0
x=0
y=0
#而p2 当p1的值,第二个while循环中的条件访问sch[p1]
,这可能超出给定数组的界限。您需要将p1保存在临时变量中,并在更新p1
的值之前使用该值
或者,您需要一个条件来检查边或边界情况。p2也是如此,它在第二个内循环中递增,第一个内循环可能试图在索引超出范围的情况下访问它。请提供如何调用这些函数的示例。当您将p2
递增到sch2
的长度时,您将继续运行内循环,因此,它将尝试访问p2
列表之外的内容。是的,但在功能上,即使是p1
也会抛出一个错误,对吗?没错,它将尝试在第二个if
中使用p1
。使用elif
也可以解决这个问题。哦,实际上我在p1时使用过
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