简体   繁体   中英

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!

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.

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM