logo
Loading...

int64, float64, object 的問題 - Cupoy

您好,我執行此程式時不能執行的原因是?

ml100-2,formatting string,軟體,語法,ml100-2-d18

int64, float64, object 的問題

2019/05/02 下午 08:52
機器學習共學討論版
Yun-Jung Hsu
觀看數:6
回答數:2
收藏數:0
ml100-2
formatting string
軟體
語法
ml100-2-d18


您好,我執行此程式時不能執行的原因是?

回答列表

  • 2019/05/02 下午 10:47
    Seanyu.TW
    贊同數:1
    不贊同數:0
    留言數:2

    Hi, 因為 print 的裡面要不是全文字,要不是用變數。若要混合的話,可以用 % 或是 .format

    var = "world"

    比方說 print("Hello %s" % var)

    或是 print("Hello {}".format(var))

  • 2019/05/02 下午 11:40
    張維元 (WeiYuan)
    贊同數:1
    不贊同數:0
    留言數:1

    嗨, f'...' 是一種新的 formatting string 的用法,python 3.6+ 才支持,所以看起來是你的版本不夠新!以下分別是用兩個版本的執行結果:


    ```

    (py3) ➜ python

    Python 3.6.5 (default, Jun 17 2018, 19:45:51)

    [GCC 4.2.1 Compatible Apple LLVM 9.1.0 (clang-902.0.39.2)] on darwin

    Type "help", "copyright", "credits" or "license" for more information.

    >>> print(f'{len([1,2,3])} In: {[1,2,3]}')

    3 In: [1, 2, 3]

    ```


    ```

    (py2) ➜ python

    Python 2.7.13 (default, Jun 17 2018, 19:47:56)

    [GCC 4.2.1 Compatible Apple LLVM 9.1.0 (clang-902.0.39.2)] on darwin

    Type "help", "copyright", "credits" or "license" for more information.

    >>> print(f'{len([1,2,3])} In: {[1,2,3]}')

      File "<stdin>", line 1

        print(f'{len([1,2,3])} In: {[1,2,3]}')

                                            ^

    SyntaxError: invalid syntax

    ```