0

I want to create a function that checks if a string variable contains ".pdf" at the end and if not, to add it to the variable. When I run this, the output_name does not change. It works outside of a function just fine but I want to create a function so I don't repeat myself. Thanks for the help!

def check(output_name):
    if ".pdf" not in output_name:
        output_name += ".pdf"
    else:
        return None

output_name = "sample"
check(output_name)
print(output_name)
Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214

4 Answers4

0
def check_ext(x):
    return x if x[-4:] == ".pdf" else x+".pdf"

This could be also a good solution, as referenced above by @Chris_Rands

def check_ext1(str):
    return str if str.endswith(".pdf") else str+".pdf"

Output

st= "string_variable"
print(check_ext(st))   # string_variable.pdf
print(check_ext1(st))  # string_variable.pdf

Hope it helps

MasterOfTheHouse
  • 1,164
  • 1
  • 14
  • 34
  • That's not a good answer since he wants by reference and not using a return statement. Just like in the @JohnnyMopp provided. – Raphael Jan 14 '20 at 17:56
  • 1
    @Raphael Well tough, Python doesn’t *do* by reference, and even if it’s possible to circumvent it, this would be a *bad* solution. This answer provides the *correct* solution. – Konrad Rudolph Jan 14 '20 at 18:01
  • @ I know. But the answer is incomplete. You can change a list in a function by reference only, without returning it: https://stackoverflow.com/a/986145 – Raphael Jan 14 '20 at 18:02
  • @Raphael No. That is simply not a good solution. Mentioning here doesn’t make this answer better, on the contrary. Don’t teach bad code just to stay literal to what was asked, focus on teaching good code instead. – Konrad Rudolph Jan 14 '20 at 18:03
  • I agree, but I still think the answer is incomplete. He should explain that the string can't change by reference since it's immutable etc. – Raphael Jan 14 '20 at 18:05
0

Try this (Note edit is based on recommendation from the comment)

def check(output_name):

    if not output_name.endswith(".pdf"): 
        return output_name + ".pdf"

output_name = "sample.pdf"    
print(check(output_name))
print(output_name)

This line (output_name += ".pdf") runs but you don't get anything because you didn't tell the function to return it. And you can't alter a variable in a function without an extra effort. Read about scopes.

If you really don't want the function returning anything or you want the variable to be alter by the function do this: WARNING => This is a bad practice!


output_name = "sample.pdf"

def check():
    global output_name  

    if not output_name.endswith(".pdf"): output_name += ".pdf"


check()
print(output_name)
Kweweli
  • 327
  • 3
  • 7
  • Using `if …: pass` is convoluted and unnecessary. You could simply write `if not output_name.endswith(".pdf"): output_name += ".pdf"`. Besides that this answer doesn’t really add anything over existing answers. – Konrad Rudolph Jan 14 '20 at 18:05
0

Method 1: based on your code.

You have almost found the solution. Here, I refactor your code to make it work.

def check(output_name):
    if ".pdf" not in output_name:
        output_name += ".pdf"
    return output_name


output_name = "sample"
result = check(output_name)
print(result) # sample.pdf

Method 2: base on the string method endswith

Here is a simple way to achieve your goal by using the str method endswith.

def check(output_name):
    if not output_name.endswith('.pdf'):
        output_name = output_name + ".pdf"
    return output_name

output_name = "sample"
result = check(output_name)
print(result) # sample.pdf
codrelphi
  • 1,075
  • 1
  • 7
  • 13
-2

I suggest the following solution

def filename_editor():
    global output_name
    if ".pdf" not in output_name:
        output_name += ".pdf"

    global input_name
    if ".pdf" not in input_name:
        input_name += ".pdf"
B--rian
  • 5,578
  • 10
  • 38
  • 89