我是一个完全的初学者,所以如果你对它的简单感到不快,请跳过这篇文章。
我不知道如何编写一个带有两个参数(name和course)并打印出来的函数:欢迎"name“到"course”训练营。
def greeting('name', 'course'):
print (welcome,'name', to, the, 'course')我希望能够打印欢迎Stacie到python训练营!
发布于 2019-10-01 14:21:16
请尝试下面这样的东西。
def greeting(name, course):
print ('welcome' + name + 'to' + 'the' + course)
greeting('Stacie', 'python')如果你仍然得到任何错误,请分享错误的截图。
发布于 2019-10-01 12:57:08
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}时,它将从变量本身获取名称。
发布于 2019-10-01 13:00:21
声明的函数参数需要是变量,而不是实际值。
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 coursehttps://stackoverflow.com/questions/58178245
复制相似问题