gpt4 book ai didi

字符串上的 Python 语法无效?

转载 作者:太空宇宙 更新时间:2023-11-03 13:14:17 25 4
gpt4 key购买 nike

我对编码很陌生,所以我希望我的问题有意义/格式正确。

这是我的代码:

#this function takes a name and a town, and returns it as the following:
#"Hello. My name is [name]. I love the weather in [town name]."

def introduction("name","town"):
phrase1='Hello. My name is '
phrase2='I love the weather in '
return ("phrase1" + "name" + "phrase2" + "town")

例如introduction("George","Washington") 在 python shell 中将返回 “你好。我叫乔治。我喜欢华盛顿的天气。”

我的问题是我的代码没有这样做。相反,我收到以下错误:

Invalid syntax: <string> on line 1". In Wing101 **,"town"): 

下面有一个红色的swigle。我不确定我做错了什么......我知道“name”和“town”是字符串而不是变量,但我真的不知道如何修复它。

感谢任何帮助/评论。

最佳答案

您不能将字符串文字用作函数参数,不。您只能使用变量名。删除所有双引号:

def introduction(name, town):
phrase1='Hello. My name is '
phrase2='. I love the weather in '
return (phrase1 + name + phrase2 + town)

调用函数时传入字符串:

introduction("George", "Washington") 

然后将它们分别分配给 nametown

关于字符串上的 Python 语法无效?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34970049/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com