vendredi 12 mai 2017

How to create a function that prints or returns results of another function?

I've created a function (p_r) that will either print or return the results of functions.

I'm doing this for running tests. (print shows the output, return will spit out the values but doesn't print the output). - Python noob; so, let me know if there is an easier way of doing this, like a "verbose" or testing keyword. I've been on this project for days.

Modules required...

import random

Variables:

yt_url = '' #url of either a video or playlist [user input]
printr_return = True
yt_url1 = 'https://youtu.be/WaGRTixwkSQ'
yt_url2 = 'https://www.youtube.com/playlist?list=PL6FhCd_HO_AD-22-Csv-vYlhLoKlAY3Zt'

The function:

#Switches between printing or returning data, depending on printr_return
#''' 
def p_r (statement, printr_return):
    if printr_return is True:
        print (statement)
    else:
        return (statement)
#'''


The problem:

I don't understand why I get an error when I use p_r in the following function:

#Choses between the two urls above and outputs yt_url
#'''
def random_urltest (yt_url1, yt_url2):
    rnt = random.choice([1,2])
    if rnt == 1:
        yt_url = yt_url1
    if rnt == 2:
        yt_url = yt_url2
    p_r (yt_url, False)    #-- what's wrong here?
    #return yt_url          #-- but this works
#'''

Full Call:

yt_url = random_urltest (yt_url1, yt_url2) #produces error if I use p_r
p_r ('Running test on url...%s' % (yt_url), True) #This works...




Aucun commentaire:

Enregistrer un commentaire