0
def absolutevalue(num):
        if num >= 0:
            abs_num = num
        else:
            abs_num = -num
        print("The absolute value"+ abs_num)

If I try to run the function absolutevalue(4), it throws error as follows

Traceback (most recent call last):

  File "<ipython-input-16-36bd355eb83d>", line 1, in <module>
    absolutevalue(4)

  File "<ipython-input-15-42a3de37c325>", line 6, in absolutevalue
    print("The absolute value"+ abs_num)

TypeError: must be str, not int

3 Answers 3

1
print("The absolute value"+ abs_num)

Python is giving you an error in this line because you can use + operator only to concatenate two string or add two integers together. You can't add integer to a string or concatenate an string with integer. It's bad syntax in Python.

You can fix this by using the string version of the integer.

print("The absolute value"+ str(abs_num))
Sign up to request clarification or add additional context in comments.

Comments

0

You need to change this line

print("The absolute value"+ abs_num)

to be

print("The absolute value", abs_num)

Comments

0

The efficient and quick way to do this is:

print("The absolute value {}".format(abs_num))

And in the latest version of Python which is 3.7 we have formatted strings now:

print(f'The absolute value {abs_num}')

Comments

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.