0
def project_to_distance(point_x, point_y, distance):
    dist_to_origin = math.square_root(point_x ** 2 + point_y ** 2)    
        scale = distance / dist_to_origin
    print point_x * scale, point_y * scale

project_to_distance(2, 7, 4)

I get the following error on the scale line (shown below). Can anyone please advise what this pertains to?

SyntaxError: bad input ('        ')
4
  • 1
    Why is the line scale = distance / dist_to_origin indented more than the lines before and after? Commented Oct 17, 2013 at 15:21
  • I fixed it. Thanks so much. How do I upvote you? Commented Oct 17, 2013 at 15:24
  • @user2603139 You can't upvote comments till you have more reputation. Commented Oct 17, 2013 at 15:24
  • This is from an on-line course in coursera.org. Commented Jan 21, 2016 at 8:48

3 Answers 3

4

I see a couple of issues:

  1. Indentation of scale = distance / dist_to_origin
  2. math.square_root does not exist, it is math.sqrt

Code:

import math

def project_to_distance(point_x, point_y, distance):
    dist_to_origin = math.sqrt(point_x ** 2 + point_y ** 2)    
    scale = distance / dist_to_origin
    print point_x * scale, point_y * scale

project_to_distance(2, 7, 4)
Sign up to request clarification or add additional context in comments.

Comments

3

There are a few problems with your code.

Here is the revised code I've provided for you:

import math

def project_to_distance(point_x, point_y, distance):
    dist_to_origin = math.sqrt(point_x ** 2 + point_y ** 2)    
    scale = distance / dist_to_origin
    return point_x * scale, point_y * scale

print project_to_distance(2, 7, 4)
  • Why is "import math" included? On the off-chance you don't know about importing, you need to include the math module in order to use advanced functions.

  • Where is my square_root? math.square_root() does not exist- the function you mean to call is math.sqrt().

  • Why did I get SyntaxError: bad input (' ')? Because in Python, whitespace (indents) are considered part of the syntax, so that programs in Python are always easier to read. You have the line scale = distance / dist_to_origin indented too far, and it confuses the Python compiler.

  • Why did you change print to return at the end of project_to_distance()? This is a higher programming concept -- early excercises teach you to print so that you can see your results, but unfortunately it confuses the subject of returning a value. Normally, you would put return at the end of a function because you don't always want to print. For example, math.sqrt() is a function just like project_to_distance(). Only, it does not print, it calculates and returns the value. Relate project_to_distance() to sqrt() and you will understand why return is more valuable.

  • Why did you add print to the end of the code? Because now that your function returns, assuming you want it to print, you have to tell it to. But now, when you run a program, you can run project_to_distance and use it as a tool in later work, rather than an always-printing function.

Happy coding.

Bonus: Here is an amazing Python tutorial

Comments

1

It works fine for me when I write the following :

def project_to_distance(point_x, point_y, distance):
    dist_to_origin = math.sqrt(point_x ** 2 + point_y ** 2)    
    scale = distance / dist_to_origin
    print point_x * scale, point_y * scale

project_to_distance(2, 7, 4)

Do not intent scale.

10 Comments

I appreciate the help guys. I'm going to vote for David Robinson since he jumped in first. Can someone advise how to do that?
@user2603139 He only commented, which doesn't really count. You can only accept answers. Besides, @jabaldonedo fixed the sqrt thing as well, so he fixed all your issues.
@Davlov Did this really work? math.square_root doesn't exist. You should have gotten an AttributeError
I know - I figured that part afterwards.
@user2603139 When you vote for an answer, please make sure to vote for the one that helps you the most, not who jumps in first. Some people will come back and give you better answers after you've already picked one, so you can change it. The point of picking a best answer is for people who come after you do, and have the same problem. Find the best answer that helped you and might help them, too. :)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.