Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Function That Receives String, Returns String + "!"

As the title suggests, I'm just trying to create a Python function that receives a string, then returns that string with an exclamation point added to the end.

An input of "Hello" should return

Hello!

An input of "Goodbye" should return

Goodbye!

Etc.

Here's what I tried:

def addExclamation(s):
    s = input("please enter a string")
    new_string = s + "!"
    return new_string

print(s(addExclamation))

This gave me the error message:

NameError: name 's' is not defined on line 6

Why is "s" not defined? I thought I determined that s is the input within the addExclamation function. Thank you for your help.

like image 214
HappyHands31 Avatar asked Jan 04 '23 21:01

HappyHands31


2 Answers

You define a function with a parameter s. That function immediately throws that value away and asks for input. You are calling a function with the same name as that parameter, and sending it an argument of the function name. That doesn't make any sense.

def addExclamation(s):
    new_string = s + "!"
    return new_string

print(addExclamation('Hello'))

Or:

def addExclamation():
    s = input("please enter a string")
    new_string = s + "!"
    return new_string

print(addExclamation())
like image 104
TigerhawkT3 Avatar answered Jan 11 '23 15:01

TigerhawkT3


You have mixed up the function and the argument here:

print(s(addExclamation))

And, you probably meant to read the input outside of a function and pass the string into:

def addExclamation(s):
    new_string = s + "!"
    return new_string

s = input("please enter a string")
print(addExclamation(s))
like image 20
alecxe Avatar answered Jan 11 '23 17:01

alecxe