Strings in function returns error. Don't know why


#1



Strings in functions String_function


Oops, try again. string_function('Hello') returned 'HelloHello' instead of 'Helloworld'


I'm lost. Can't see how this error is possible. Works in a stand alone Python Interpreter.

Help appreciated
Peter


#n = "Hello"
# Your function here!
def string_function(s):
    n = "Hello"
    s = n + s
    return s

s = "world"
print string_function(s)


#2

def string_function(s):
    n = "Hello"
    s = n + s
    return s

s = "world"
print string_function(s)

The way your function is built is the problem there is a much simpler way of performing this action and i will show you how. :slight_smile:

#Create global variable n that contains string "hello"

n = "Hello"

def string_function(s):

#Use only one return statement for function
    return s + 'world'

#Call your function
print string_function(n)

#3

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.