Reputation: 22114
I'm required to do a python quiz.
here is question:
Your challenge here is to write a function format_point that returns a string representing a point in 2-space. The function takes three parameters. The first two are floating point numbers representing the x and y coordinates of a point and the third parameter is an integer specifying the required number of digits after the decimal point. The returned string is of the form "(23.176, 19.235)". For example, the following three lines of code should print the output (0.67, 17.12).
What I did was:
>>> def coordinate(x,y,n):
... str_x = format(x,"."+n+"f")
... str_y = format(y,"."+n+"f")
... print("("+str_x+","+str_y+")")
...
>>> coordinate(10.242,53.124,2)
I got the error:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 2, in coordinate
TypeError: cannot concatenate 'str' and 'int' objects
Where I did wrong?
Upvotes: 0
Views: 148
Reputation: 5217
cannot concatenate 'str' and 'int' objects
Try
format(str(x), "." + str(n) + "f")
or
format(str(x), ".%sf" % n)
Upvotes: 3