Jump to content
Search In
  • More options...
Find results that contain...
Find results in...

I'm using codecademy to learn the basics of Python and was wondering why they had me doing things certain ways even if my way put out the same output. 

 

The example I'm at is as follows:


 

print "The value of pi is around " + 3.14

it wants me to turn 3.14 into a string using str() like this;

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

but I don't understand why I couldn't have it like this;

print "The value of pi is around " + "3.14"

Would both ways work and one is more correct than the other? 

I've dealt with some programming before but was just curious about this.

Watch out behind you!

Link to post
Share on other sites

It wants you to do it like that because if 3.14 was a variable you wouldn't be able to just add quotes around it. The proper way to do it if you have a literal number like that would be to just put it inside the rest of the string.

print "The value of pi is around 3.14"

 

The trick to doing code academy is hitting the little x on the tab and finding a better place to learn, preferably a place that teaches Python 3.

1474412270.2748842

Link to post
Share on other sites

you can get into the argument of which is more pythonic but truthfully you can do whatever if it suits your needs though as the snake above said it just wants to be sure you get it as a text not a variable or number. 

I also agree you should learn from a better source or atleast look at other sources as well especially for python 3-3.5 

in python 3 

pi = 3.14159265

print ('The value of pi is',pi)

                     .
                   _/ V\
                  / /  /
                <<    |
                ,/    ]
              ,/      ]
            ,/        |
           /    \  \ /
          /      | | |
    ______|   __/_/| |
   /_______\______}\__}  

Spoiler

[i7-7700k@5Ghz | MSI Z270 M7 | 16GB 3000 GEIL EVOX | STRIX ROG 1060 OC 6G | EVGA G2 650W | ROSEWILL B2 SPIRIT | SANDISK 256GB M2 | 4x 1TB Seagate Barracudas RAID 10 ]

[i3-4360 | mini-itx potato | 4gb DDR3-1600 | 8tb wd red | 250gb seagate| Debian 9 ]

[Dell Inspiron 15 5567] 

 

 

Link to post
Share on other sites

Not sure how casting works in Python, but I think they want you to do it in that specific way so you can learn how to cast.

 

The first form is to turn a variable with the float type into a string.

The second form is to hard code it into the desired string.

 

The first will teach you how to cast, and you can swap

str()

for other things, such as converting an integer into a

float()

, presumably. Oh, and you can also swap

3.14

with a variable name, such as

str(pi)

if you had defined pi as a constant.

Doing it way #2 won't, I think with my limited knowledge of Python.

i5 3570K @ 4.3Ghz Turbo (OC) | GIGABYTE G1 GTX 980 Ti | G.Skill 8GB RAM | Samsung 850 EVO 2TB

Link to post
Share on other sites
print("The value of pi is around", 3.14)
print("The value of pi is around %f" % 3.14)
print("The value of pi is around {0}".format(3.14))

 

Desktop: Corsair RM550x | Ryzen R9 3900X |RTX 2080 (borrowed from work) - MSI GTX 1080 | 64GB 3600MHz CL16 memory | Asus X470-F Gaming | Samsung 970 EVO 512GB | Samsung 830 256GB | 3TB HDD | Corsair 450D | Corsair H100i (NF-F12 fans) | MG279Q

Laptop: Surface Pro 7 (i5, 16GB RAM, 256GB SSD)

Console: Playstation 4 Pro

Link to post
Share on other sites
On 5/21/2016 at 10:05 PM, Craftsman_2222 said:

I'm using codecademy to learn the basics of Python and was wondering why they had me doing things certain ways even if my way put out the same output. 

 

The example I'm at is as follows:


 


print "The value of pi is around " + 3.14

it wants me to turn 3.14 into a string using str() like this;


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

but I don't understand why I couldn't have it like this;


print "The value of pi is around " + "3.14"

Would both ways work and one is more correct than the other? 

I've dealt with some programming before but was just curious about this.

Codecademy can be pretty stupid sometimes in how they want you to do stuff. It's probably just so you know str() is a thing since I'm pretty sure Codecademy is meant for those with no experience.

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×