Syntax error


#1

Hey guy, anyone know why can’t I use the backlash in this case, and how I can make another line without using PRINT function. Thank you so much for your help!


#2

what the backslash does is allowing you put the code on multiply lines:

print "hello", \
"world"

if you want the output by print on multiply lines, use the new line character (\n) which needs to be part of the string

if the exercise asks for 2 print statements, you need to add them, then new line character won’t get you through


#3

Thank you so much for your help, but it seems so hard for me to know how to do. I did try, but it doesn’t work. Dont know how


#4

the new line character needs to be part of string:

print "Monday\nTuesday"

the code should be on a single line, the new line character will make the output display over two lines


#5

Now, I got it. But I still get trouble when I put the variables following. Do you know how I can fix it.

-Thank you so much-


#6

that depends on how you put the variables.

as i told you, for the exercise (to pass) you most likely need two print statements


#7

I just try, and there is still syntax error.

Thank you so much for your help


#8

what do you want? Put the code on multiply lines using the continuation character (\), which will give a single line output or use the new line character (\n) which put all code on a single line, and the output on multiply lines

given you currently use the continuation character, the code just continues, so you shouldn’t use print on line 5, correct:

print "hello",\
"world"

wrong:

print "hello",\
print "world" 

the last one is wrong, because if you would remove the continuation character you would get:

print "hello", print "world" 

which is obviously wrong. See how the continuation character works? It allows you to put the code on multiply lines, but you should read it as a single line of code


#9

I understand it, but I still get troubles when I put the variables following.


#10

I am so sorry. This is so new to me.


#11

because you put another print statement in when printing b, if we would read it as a single line of code:

print "hello", a, print "hello", b

its obviously wrong.

it should be:

print "hello", a, "hello", b

the same logic applies when using continuation character:

print "hello", a, \
"hello", b

#12

the output doesn’t give multiple lines


#13

duh? i told you, the continuation character is for the programmer to put code on multiple lines which gives a single line output, if you want the output over multiply lines, you need to use the new line character (\n), telling you want the code following it printed on a new line


#14

But where I should put \n, right after the argument or after the variables


#15

i told you, \n should be part of a string. Where you put it is up to you. You can do:

print "hello", a, "\n"

or:

"\nhello", b

you can put it where you want the line to start


#16

Ok. Finally, I got it. Thank you so so much. One more question, Do u know how I can use the END function


#17

end is introduced in python3, codecademy uses python2.


#18

Oh. Ok. But thank you so much for your patience. I dont know how i can learn without your help!.


#19

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.