3

I am very new to coding so I hope my question makes sense/is formatted correctly.

Here is my code:

#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") 

E.g. introduction("George","Washington") in the python shell would return as "Hello. My name is George. I love the weather in Washington."

My problem is that my code is not doing that. Instead I get the following error:

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

has a red swiggle under it. I'm not sure what I did wrong...I know "name" and "town" are strings and not variables, but I really have no idea how to fix it.

Any help/comments are appreciated.

2
  • I suggest you do some basic research on how to define functions, such as by reading the relevant section in the official tutorial. Commented Jan 23, 2016 at 23:11
  • I suspect this got a lot of views over time because of the "clickbait" title - people could end up here, for example, because they were trying to use eval and there was a syntax error in the evaluated code. Commented Jun 24, 2024 at 17:46

1 Answer 1

9

You can't use string literals as function arguments, no. You can only use variable names. Remove all the double quotes:

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

You pass in strings when calling the function:

introduction("George", "Washington") 

and these then get assigned to name and town respectively.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.