0

For some reason int(str) is causing an error. I can't work out why. I'm wondering if someone can tell me why.

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')

Error: invalid literal for int() with base 10: ''

1 Answer 1

3

Look closely at the condition:

d_num = ""
if process_tuple == list2[0]:
    d_num = process_tuple[2:3]

So what should happen when the if test is False (when the value is not equal to list2[0]? d_num remains an empty string and you cannot convert that to an integer.

The error message tells you exactly that; invalid literal for int() with base 10: '' tells you an empty string cannot be converted.

Your first value in the cx4_list_reduce list is '[#1]', and that string is not equal to list2[0] ('[#1] is not equal to '[#2]'), so the above equality test fails.

Sign up to request clarification or add additional context in comments.

1 Comment

Wow, I'm impressed. I spent a whole on it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.