A community for students.

Here's the question you clicked on:

55 members online
  • 0 replying
  • 0 viewing

anonymous

  • 5 years ago

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

  • This Question is Closed
  1. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    x=15 ##define if (x/2)*2==x: print 'even' ##the left of 'print' shoud be space

  2. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    I am still getting a syntax error at 'even'

  3. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    try if ((x/2)*2 ==x): print "even"

  4. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    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.

  5. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    By the way - this logic will call any number even. You need something like if x/2 == int(x/2): print('even')

  6. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    or if float(x/2) == int(x/2): print('even')

  7. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    @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.

  8. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    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.

  9. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    (x/2) * 2 is an example in the lecture to illustrate how integer division works.

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

    • Attachments:

Ask your own question

Sign Up
Find more explanations on OpenStudy
Privacy Policy

Your question is ready. Sign up for free to start getting answers.

spraguer (Moderator)
5 → View Detailed Profile

is replying to Can someone tell me what button the professor is hitting...

23

  • Teamwork 19 Teammate
  • Problem Solving 19 Hero
  • You have blocked this person.
  • ✔ You're a fan Checking fan status...

Thanks for being so helpful in mathematics. If you are getting quality help, make sure you spread the word about OpenStudy.

This is the testimonial you wrote.
You haven't written a testimonial for Owlfred.