Python 2.7: %d, %s, and float()

后端 未结 2 1447
庸人自扰
庸人自扰 2021-01-01 17:37

I am attempting to teach myself a little coding through the \"learn python the hard way\" book and am struggling with %d / %s / %r when tying to display a floating point num

2条回答
  •  北海茫月
    2021-01-01 18:23

    Try the following:

    print "First is: %f" % (first)
    print "Second is: %f" % (second)
    

    I am unsure what answer is. But apart from that, this will be:

    print "DONE: %f DIVIDED BY %f EQUALS %f, SWEET MATH BRO!" % (first, second, ans)
    

    There's a lot of text on Format String Specifiers. You can google it and get a list of specifiers. One thing I forgot to note:

    If you try this:

    print "First is: %s" % (first)
    

    It converts the float value in first to a string. So that would work as well.

提交回复
热议问题