What's wrong: Why doesn't a loop work here

In 8. Parsing Numbers form Complex Strings; Part 2
Why wouldn’t this code work:

def process_date(string):
    if '-' in string:
        for x in splitted:
        return div_round_2
#        numb_1=int(splitted[0])
#        numb_2=int(splitted[1])
#        division=(numb_1+numb_2)/2
#        div_round=round(division)
#        return div_round
        return string

I expect it to turn both Strings to turn into integers.

But it seems not:

TypeError                                 Traceback (most recent call last)
<ipython-input-1-4516fb67e44c> in <module>
     38 processed_test_data=[]
     39 for string in stripped_test_data:
---> 40     date = process_date(string)
     41     processed_test_data.append(date)

<ipython-input-1-4516fb67e44c> in process_date(string)
     24         for x in splitted:
     25              x=int(x)
---> 26         division_1=(sum(splitted))/2
     27         div_round_1=round(division_1)
     28         return div_round_2

TypeError: unsupported operand type(s) for +: 'int' and 'str'

Here you are assigning value to variable x which has local scope (within loop only).

You will need to alter splitted list like

for index in range(len(splitted)):
    splitted[index] = int(splitted)
1 Like

Thank you, understood.

1 Like

Glad it help you. :slightly_smiling_face: Please consider to mark it as solution or give like if it solved issue.

If you are new here you can refer to these technical guideline for future question