This question already has an answer here:
- Python: Function always returns None 4 answers
- Why does my function return None? 4 answers
Below is a recursive function I wrote to generate random numbers in a range. The trouble is, it sometimes returns "none". Why is that?
import random
# Here, 'data' is a list of images. It contains about 20000 images.
# But I want to pick an image at random within index 13338, not beyond.
# hence the need for this function.
def choose_random_image(data):
i = random.choice(data.index)
if i in range(0,13338):
return i
else:
choose_random_image(data)
i = choose_random_image(dataset)
print(i)
It works about roughly 80-90% of the time, and the other 10-20% of the time, it prints "none".
Aucun commentaire:
Enregistrer un commentaire