anonymous
  • anonymous
Why am I getting a syntax error in this problem? >>> x = 15 ## Define x >>> if (x/2) * 2 == x: print 'even' ## define even value SyntaxError: invalid syntax
MIT 6.00 Intro Computer Science (OCW)
  • Stacey Warren - Expert brainly.com
Hey! We 've verified this expert answer for you, click below to unlock the details :)
SOLVED
At vero eos et accusamus et iusto odio dignissimos ducimus qui blanditiis praesentium voluptatum deleniti atque corrupti quos dolores et quas molestias excepturi sint occaecati cupiditate non provident, similique sunt in culpa qui officia deserunt mollitia animi, id est laborum et dolorum fuga. Et harum quidem rerum facilis est et expedita distinctio. Nam libero tempore, cum soluta nobis est eligendi optio cumque nihil impedit quo minus id quod maxime placeat facere possimus, omnis voluptas assumenda est, omnis dolor repellendus. Itaque earum rerum hic tenetur a sapiente delectus, ut aut reiciendis voluptatibus maiores alias consequatur aut perferendis doloribus asperiores repellat.
katieb
  • katieb
I got my questions answered at brainly.com in under 10 minutes. Go to brainly.com now for free help!
anonymous
  • anonymous
x=15 ##define if (x/2)*2==x: print 'even' ##the left of 'print' shoud be space
anonymous
  • anonymous
I am still getting a syntax error at 'even'
anonymous
  • anonymous
try if ((x/2)*2 ==x): print "even"

Looking for something else?

Not the answer you are looking for? Search for more explanations.

More answers

anonymous
  • anonymous
This code works correctly. http://dpaste.com/518071/ If you're getting a syntax error, either you're using python 3 or you have some funky issues with spacing or non-printing characters. My guess is you're using python 3. If that's the case you can try using print('even') and see if that works better. If you're not using python 3. Try retyping your code, but be sure that you indent (with a tab or 3-4 spaces) the line after the if (x/2)*2 == x: line.
anonymous
  • anonymous
By the way - this logic will call any number even. You need something like if x/2 == int(x/2): print('even')
anonymous
  • anonymous
or if float(x/2) == int(x/2): print('even')
anonymous
  • anonymous
@JeffA (15/2) == 7 so (15/2) * 2 == 14 != 15 So the math is fine and the problem is probably indentation or python 3. But it's not terribly versatile since it will call all numbers even if x is a float.
anonymous
  • anonymous
Indeed. Probably best to use modulo, but the instructor hasn't really covered it in the first lecture or two, so that's why most people use the (x/2) * 2 method.
anonymous
  • anonymous
(x/2) * 2 is an example in the lecture to illustrate how integer division works.

Looking for something else?

Not the answer you are looking for? Search for more explanations.