Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

I can't get python to return the values of the function

I can’t get python to return the values of the function I have tried joining the function variables into one and I test the values separately with python print and they work but in the test it only returns the value none.

this is the code.

import codecs
import os
import re

a = str(r"'c:\videos\my video.mp4'")
b = a.replace('\\','/')
real_direct=b.replace(' ','')
max_caract = int(len(real_direct)-1)  
def dividir(i) :
   if real_direct[i]=='/' :
       print(real_direct[i])
       direct = real_direct[2 : i+1] 
       file = real_direct[i+1 : max_caract]
       #print(direct)
       #print(file)
       return (direct, file)
   else: 
        dividir(i-1)

print(dividir(max_caract))

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

The problem here is that you are calling a function recursively, and its inner-most function call is the only thing that returns anything. Everything else, including the dividir() that is being printed, return nothing. return dividir(i-1) in the else solves it.

#!/usr/bin/env python3

import codecs
import os
import re
from pathlib import Path

a = str(r"'c:\videos\my video.mp4'")
b = a.replace("\\", "/")
real_direct = b.replace(" ", "")
max_caract = int(len(real_direct) - 1)


def dividir(i):
    print('starting dividir function')
    if real_direct[i] == "/":
        print(f'{real_direct[i]=}')
        direct = real_direct[2 : i + 1]
        file = real_direct[i + 1 : max_caract]
        print(f'{direct=}')
        print(f'{file=}')
        return (direct, file)
    else:
        print('doing else')
        return dividir(i - 1)



print(dividir(max_caract))
$ ./codecs.py
starting dividir function
doing else
starting dividir function
doing else
starting dividir function
doing else
starting dividir function
doing else
starting dividir function
doing else
starting dividir function
doing else
starting dividir function
doing else
starting dividir function
doing else
starting dividir function
doing else
starting dividir function
doing else
starting dividir function
doing else
starting dividir function
doing else
starting dividir function
real_direct[i]='/'
direct=':/videos/'
file='myvideo.mp4'
(':/videos/', 'myvideo.mp4')
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading