Why is this printing | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Why is this printing

in this code x=5 y=6 if x: ("cool") print ( x+y) it shouldn't print anything but it does and if I add- else : print ("aww") it says I have a syntaxt error for else : even if I change the spacing. why

23rd Jan 2017, 10:07 PM
John Ahmrain
John Ahmrain - avatar
4 Answers
+ 1
First rule of python, code blocks are defined by indentations! Your code doesn't have any indents!!! So here is what I see posted in this thread: x=5 y=6 if x: ("cool") print ( x+y) If you throw this in the code playground, the output is "11" and if you really meant to keep that "print()" statement in your code, the output should be eleven. As for the error, the "else" statement is dangling. What do I mean by dangling? Since Python uses indents to enforce code blocks, you have an "if" statement that does nothing, a print statement that follows (that's why you get the output 11), and later an "else" statement that is not associated with an "if" statement hence the dangling part. To fix that, you need to add indents to your code like such: x=5 y=6 if x: ("cool") print ( x+y) else : print ("aww") Now I have another question for you. What are you trying to do on the line: if x: ("cool") ??? It looks to have no purpose. So why include it on the line? In all, go back and add the indents then try running your code again. You should find that it works without errors when you include the "else".
24th Jan 2017, 8:05 PM
M King
M King - avatar
0
im actually trying to figure something else out and im not getting the code to do anything so this was a test to figure out what is going on. specifically: im trying to figure out the easiest way to combine the if/and statements of python with the ability to change font color in css. I know you can do that with js, but im not ready to learn js yet cause I still havent fig out the css and python that ive already started. I did find somethings on font colors for python but it was absurdly complex for what I want to use it for. again specifically: creating patterns of text color to create a rainbow effect for my writing.
24th Jan 2017, 12:05 AM
John Ahmrain
John Ahmrain - avatar
0
also, thatgets the result unexpected indent for error on the line with print (x+y)
24th Jan 2017, 12:08 AM
John Ahmrain
John Ahmrain - avatar
0
I figured it out. it should be; x=5 y=6 if x==("cool"): print ("whatever") else : print ("aww")
24th Jan 2017, 12:24 AM
John Ahmrain
John Ahmrain - avatar