A.J*_*.J. 3 python syntax python-2.7
我对编码很新,所以我希望我的问题有意义/格式正确.
这是我的代码:
#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")
Run Code Online (Sandbox Code Playgroud)
例如introduction("George","Washington")在python shell中将返回as"Hello. My name is George. I love the weather in Washington."
我的问题是我的代码没有这样做.相反,我收到以下错误:
Invalid syntax: <string> on line 1". In Wing101 **,"town"):
Run Code Online (Sandbox Code Playgroud)
在它下面有一个红色的摇摆.我不确定我做错了什么......我知道"名字"和"城镇"是字符串而不是变量,但我真的不知道如何解决它.
任何帮助/意见表示赞赏.
你不能使用字符串文字作为函数参数,不.您只能使用变量名称.删除所有双引号:
def introduction(name, town):
phrase1='Hello. My name is '
phrase2='. I love the weather in '
return (phrase1 + name + phrase2 + town)
Run Code Online (Sandbox Code Playgroud)
调用函数时传入字符串:
introduction("George", "Washington")
Run Code Online (Sandbox Code Playgroud)
这些然后会被分配给name和town分别.