由于某种原因,int(str)导致错误。我无法理解为什么。我想知道是否有人可以告诉我原因。
cx4_list_reduce = ['[#1]',(1,3,5),(7,6,9)]
list2= ['[#2]',(2,5,4), (1,3,5), (5,8,1), (7,2,6)]
n2 =3
for process_tuple in cx4_list_reduce:
d_num = ""
if process_tuple == list2[0]:
d_num = process_tuple[2:3]
n1 = int(d_num)
if n1 <= n2:
print('n1 =< n2')
continue
else:
print('n1 => n2')
错误:invalid literal for int() with base 10: ''
答案 0 :(得分:3)
仔细观察病情:
d_num = ""
if process_tuple == list2[0]:
d_num = process_tuple[2:3]
那么当if
测试为False
时(当值不等于list2[0]
时会发生什么?d_num
仍为空字符串,您无法将其转换为一个整数。
错误消息确切地告诉您; invalid literal for int() with base 10: ''
告诉您无法转换空字符串。
cx4_list_reduce
列表中的第一个值为'[#1]'
,该字符串不等于list2[0]
('[#1]
不等于'[#2]'
),所以上述等式测试失败了。