Home > Software design >  code does not work in sublime text when using function with "return" statement
code does not work in sublime text when using function with "return" statement

Time:03-30

I'm new to python programming, and as a beginner I want to start by using a code editor, I choose sublime text 4 but I face this problem, So help me please ! This is the code :

def return_string(your_string):
    if len(your_string) >= 4:
        new_string = your_string[0:2]   your_string[-2:]
        return new_string
    elif len(your_string) == 2:
        new_string = your_string * 2
        return new_string
    elif len(your_string) < 2:
        new_string = ""
        return new_string

return_string("welcome")**

the expected output is "weme" but I get nothing in sublime text output (when I click Ctrl B).

When I change return to print the code is executed properly.

problem with no problem with

By the way the code above works in vscode without any problem.

CodePudding user response:

Because "return string" returns a string, you must first save the data in a variable, which you may then use later in the program.

result_string = return_string("welcome")
print(result_string)

CodePudding user response:

Python doesn't print outside the REPL normally, unless you explicitly tell it to. Add a call to print on your last line:

print(return_string('welcome'))

This is why adding an explicit print in your function works.

You can store the value into a variable first if you want to use it elsewhere:

result = return_string('welcome')
print(result)
  • Related