Reputation: 9
I am trying to output
Grace was planning a dream vacation to Paris.
Grace was especially looking forward to trying the local
cuisine, including stinky soup and bananas.
Grace will have to practice the language quietly to
make it easier to jump with people.
Grace has a long list of sights to see, including the
button museum and the blue park.
and with some of those words being variables its hard to put it in a print as im still new. Here is my code and error message
print("Let's play Sill Sentences!")
name=str(input("Enter a name: "))
abj1=str(input("Enter an adjective: "))
abj2=str(input("Enter a different adjective: "))
food1=str(input("Enter a food: "))
food2=str(input("Enter another food: "))
noun=str(input("Enter a noun: "))
place=str(input("Enter a place: "))
verb=str(input("Enter a verb: "))
print( , name + "was planning a dream vacation to " ,place +".",name+ "was especially looking forward to trying the local cusine,including ",abj1+ ,food1+"and ",food2)
Then my error code is SyntaxError: bad input on line 11 My question is how do you properly input mutiple varibles, like this,within a print and still have a succseful code. Again im very new and trying to learn Python.
I tried to put the (input) commands with str() but that hasnt worked and im not sure how to put the variables with the print command.
Upvotes: 1
Views: 148
Reputation: 66
print( , name + "was planning a dream vacation to " ,place +".",name+ "was especially looking forward to trying the local cusine,including ",abj1+ ,food1+"and ",food2)
This is wrong. First of all, ypu put a comma at the start. Remove it.
print( name + "was planning a dream vacation to " ,place +".",name+ "was especially looking forward to trying the local cusine,including ",abj1+ ,food1+"and ",food2)
Then replace + with a comma. It should work and if it didn't, tell me and I will try to change it. It will be like this:
print( name , " was planning a dream vacation to " ,place,". ",name, " was especially looking forward to trying the local cusine,including ",abj1 ,food1," and ",food2)
I tried it, it's working. What you did wrong was the + sign, because you have to use a comma instead of a + sign.
Upvotes: -1
Reputation: 1255
The problem in here is the first comma. Python doesn't allow commas with nothing before them. There is a such a comma in the very beginning, after print(
and there's an other one in abj1+ ,food1
. The example won't crash anymore when you these commas.
However, the output will look as follows:
Let's play Sill Sentences!
Enter a name: Grace
Enter an adjective: stinky
Enter a different adjective: blue
Enter a food: soup
Enter another food: bananas
Enter a noun: museum
Enter a place: Paris
Enter a verb: jump
Gracewas planning a dream vacation to Paris. Gracewas especially looking forward to trying the local cusine,including stinkysoupand bananas
That's not exactly wat we expected.
The thing you should always remember, is that commas insert a space between the words, while plus signs don't. So, where you want a space, you should place a comma, and where you want the words to be sticked together, a plus sign.
After changing this correctly, we get:
# I'm ommitting the inputs for readability and shortness, you still have to add them.
print(name, "was planning a dream vacation to", place + ".", name, "was especially looking forward to trying the local cuisine, including", abj1, food1, "and", food2)
Additionally, the same can be achieved with using plus signs everywhere and placing spaces after and before every string. Do note that we have to insert + " " +
between adj1
and food1
, because we want a space between those two words.
print(name + " was planning a dream vacation to " + place + ". " + name + " was especially looking forward to trying the local cuisine, including " + abj1 + " " + food1 + " and " + food2)
Now the output changes to:
Grace was planning a dream vacation to Paris. Grace was especially looking forward to trying the local cuisine, including stinky soup and bananas
The second problem is that this code doesn't insert any newlines. This problem can be solved in two different ways:
The first way is to create a different print statement for each line you want to print. The print
function will automatically insert a newline after each time something is printed. As such, the text of every new print call will be placed on a new line.
We would have to change the code to the following:
print(name, "was planning a dream vacation to", place + ".")
print(name, "was especially looking forward to trying the local")
print("cuisine, including", abj1, food1, "and", food2)
You can add empty lines by placing empty print statements, print()
. This is the same as print("")
or print(" ")
.
The second way is using the newline character, \n
. On every place where you place this in a string, the following text will be placed on a new line. Be sure that you use a backslash (\
) and not a forward one (/
). You can make empty lines by placing multiple \n
's after eachother, by example \n\n
.
We would have to change the code to the following:
print(name, "was planning a dream vacation to", place + ".\n" + name, "was especially looking forward to trying the local \ncuisine, including", abj1, food1, "and", food2)
Do note that we have to use a plus sign after ".\n"
, because otherwise the second line will start with a space.
Both pieces of code give the following output:
Grace was planning a dream vacation to Paris.
Grace was especially looking forward to trying the local
cuisine, including stinky soup and bananas
Upvotes: 3