Help on 13


#1

Hi.This is my code that I have.

# Turn 3.14 into a string on line 3!

print "The value of pi is around " + str(2) + "3.14"

The error pops out as:
Oops, try again. It looks like you printed "The value of pi is around 23.14" instead of "The value of pi is around 3.14".

I am required to:
1. Run the code as-is. You get an error!
2. Use str() to turn 3.14 into a string. Then run the code again.

This is in the hint section:
The code to turn the number 2 into a string is str(2).
Can you convert the number 3.14 into a string?

Please help quickly. I am at a competition with my class to see who can finish the course first. Thanks in advance.


#2

When you type str(2) you're turning 2 into a string. Since 3.14 is already a string it adds the 2 to the string to get 23.14.


Just looked at it the exercise. The original code is:

print "The value of pi is around " + 3.14

so if str(2) turns the integer 2 into a string, how would you turn the float 3.14 into a string without adding the "? Good luck beating the class! :slight_smile:


#3

Also, why are you putting str(2) in the code? It doesn't need that.


#4

Thanks. I just hung upped on the 2 part as seen here (in bold):
Sometimes you need to combine a string with something that isn't a string. In order to do that, you have to convert the non-string into a string.

print "I have " + str(2) + " coconuts!"
This will print I have 2 coconuts!.

The str() method converts non-strings into strings. In the above example, you convert the number 2strong text into a string and then you concatenate the strings together just like in the previous exercise.

Now try it yourself!
It was actually correct but I just had the 2 in the string so the system thought it was part of the string. Thanks again!


#5

Turn 3.14 into a string on line 3!

pi="3.14"
print "The value of pi is around " + str(pi)