Home > Mobile >  Insert a function in a sentence to be printed with placeholders
Insert a function in a sentence to be printed with placeholders

Time:08-15

I'm just starting out in Python. I have a problem, I think very simple, with a function.

My goal is to print a string variable Template_1 with a variable substring (a substring defined by the string variable example1 or example2). I would like to select only one of the substring options based on user input. I have not posted the main file so as not to lengthen the question; even so it's very simple.

This code works correctly, with user input viene_da to function template:

    example1 = "Text 1"
    example2 = "Text 2"
    
    #viene_da is chosen with the selection of the combobox items in the main file
    def template(viene_da):
        Template_1 =  "{date}."   \
                       (example1 if viene_da == "a" else "")   \
                       (example2 if viene_da == "b" else "")   \
                       "{sujbect}, {verb}"               
        return Template_1

Now I would like to create a function to select the right substring and call it in Template_1.

I wrote it like this, but there are errors:

    example1 = "Text 1"
    example2 = "Text 2"

    #viene_da is chosen with the selection of the combobox items in the main file
    def text(viene_da):
        a = (example1 if viene_da == "a" else "") 
        b = (example2 if viene_da == "b" else "")
        return x
        
    def template(viene_da):
        Template_1 =  "{date}."   \
                       text()   \ #LOOK HERE!!!
                       "{sujbect}, {verb}"   
        return Template_1

Variations of this code produce errors:

  • TypeError: text() missing 1 required positional argument: viene_da
  • NameError: name 'x' is not defined

How can I solve this? I know that I haven't shared the complete code, but for those who are experienced in Python it will be very easy to help me, because I would just like to insert a function in a sentence that prints. Thank you.

CodePudding user response:

As you're a new Python learner, for the level of code complexity you're creating I strongly suggest you familiarize yourself with conditional statements. They're logical structures built to handle the "if this, then that" logic you're trying to implement.

Your NameError mistake was trying to return some undefined x when you really wanted to return one of a or b. You can use conditional statements to determine which one you want, and then have text return it directly.

I think this modified text function here will accomplish what you're trying to achieve:

example1 = "Text 1"
example2 = "Text 2"

def text(viene_da):
    if viene_da == "a":
        return example1
    elif viene_da == "b":
        return example2
    
def template(viene_da):
    Template_1 =  "{date}."   \
                   text(viene_da)   \
                   "{sujbect}, {verb}"   
    return Template_1

Additionally, when you were calling text before you weren't passing any argument to the function. text has one argument in its definition, but defining arguments to a function is not the same as passing arguments to a function. Just because viene_da is the name of an argument in the definition of template and the name of an argument in the definition of text does not mean that viene_da is passed to text when it is passed to template.

Edit: In response to a comment from OP, when no user input is provided in the main program the following error is raised: TypeError: can only concatenate str (not "NoneType") to str

This is because the main program is calling function template and providing an empty string as the argument, which calls function text with an empty string argument. text returns the null value None, which causes an error in the template string concatenation.

To avoid this I see two primary options:

  1. Rewrite the main program to avoid calling template unless the user provides one of the expected inputs

  2. Rewrite text to return a placeholder if template is called without one of the expected inputs.

Which option is "correct" depends on the desired behavior of the main program. I don't know what the desired main program behavior is, so I can only offer guidance for option 2:

example1 = "Text 1"
example2 = "Text 2"

def text(viene_da):
    if viene_da == "a":
        return example1
    elif viene_da == "b":
        return example2
    else:
        return ""

def template(viene_da):
    Template_1 = "{date}."   \
        text(viene_da)   \
        "{sujbect}, {verb}"
    return Template_1
  • Related