编写一个带有2个字符串参数的函数

时间:2019-10-01 04:54:48

标签: python string function arguments

我是一个初学者,因此如果您对它的简单性感到恼火,请跳过此内容。

我不知道如何编写一个带有两个参数(名称和课程)并打印的函数:欢迎将“名称”添加到“课程”训练营中。

$sql1="SELECT city,phone,name from table1 where  city='NY'
UNION
SELECT city,phone,name from table2 where city='NY'";
 $result = $conn->query($sql1);

我希望能够将Welcome Stacie打印到python训练营!

3 个答案:

答案 0 :(得分:2)

请尝试以下类似方法。

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

greeting('Stacie', 'python')

如果仍然出现任何错误,请分享错误的屏幕截图。

答案 1 :(得分:1)

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

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

该函数带有两个变量,这些变量不是字符串,因此它们将不带引号。在打印语句中,在这种情况下,使用f"<text>"{}的帮助下打印字符串中的变量。因此,当您输入字符串{name}时,它将使用变量本身的名称。

答案 2 :(得分:1)

您声明的函数参数必须是变量,而不是实际的

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

请注意,引号是完全错误的。单引号会围绕一段人类可读的文本,而没有引号的内容必须是有效的Python符号或表达式。

如果要提供默认值,则可以这样做。

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

呼叫greeting()会产生

welcome John to the Python 101 course

当然要用类似的参数调用它

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

将使用您作为参数传递的值填充变量:

welcome Slartibartfast to the Pan Galactic Gargle Blaster course