stacie_labelle
stacie_labelle

Reputation: 39

write a function that takes 2 string arguments

I'm a total beginner so please skip this one if you are offended by how simple it is.

I can't figure out how to write a function which takes two arguments (name and course) and prints : Welcome "name" to the "course" bootcamp.

def greeting('name', 'course'):
    print (welcome,'name', to, the, 'course')

I want to be able to print Welcome Stacie to the python bootcamp!

Upvotes: 2

Views: 7846

Answers (3)

Damian
Damian

Reputation: 1374

def greeting(name, course):
    print (f"Welcome {name} to the {course}")

greeting("Jhon", "Python for Beginners")
# > Welcome Jhon to the Python for Beginners

The function takes two variables, the variables are not strings, so they won't have quote marks. In the print statement, f"<text>" is used in this case to print variables in the string with the help of {}. So when you type in the string {name} it will take the name from the variable itself.

Upvotes: 1

Aditya
Aditya

Reputation: 567

Pls try something like below.

def greeting(name, course):
    print ('welcome' + name + 'to' + 'the' + course)

greeting('Stacie', 'python')

if you still getting any error pls share screenshot of error.

Upvotes: 3

tripleee
tripleee

Reputation: 189307

The function arguments you declare need to be variables, not actual values.

def greeting(name, course):
    print ('welcome', name, 'to the', course)

Notice how you had the quoting diametrically wrong. The single quotes go around pieces of human-readable text and the stuff without quotes around it needs to be a valid Python symbol or expression.

If you want to supply a default value, you can do that.

def greeting(name='John', course='Python 101 course'):
    print ('welcome', name, 'to the', course)

Calling greeting() will produce

welcome John to the Python 101 course

and of course calling it with arguments like

greeting('Slartibartfast', 'Pan Galactic Gargle Blaster course')

will fill the variables with the values you passed as arguments:

welcome Slartibartfast to the Pan Galactic Gargle Blaster course

Upvotes: 1

Related Questions