1. 程式人生 > >python中,用input()輸入一個整數

python中,用input()輸入一個整數

  我想用input()輸入一個整數,結果報錯:
  TypeError: ‘str’ object cannot be interpreted as an integer

  原來input()返回的值是str,比如輸入5,其實得到的是 ‘5’
  其實只需要再用int()轉換一下,就能得到我想要的整數了。

  這裡是檢視input的help:

>>>help(input)
    Help on built-in function input in module builtins:
    input(prompt=None, /)
        Read a string
from standard input. The trailing newline is stripped. The prompt string, if given, is printed to standard output without a trailing newline before reading input. If the user hits EOF (*nix: Ctrl-D, Windows: Ctrl-Z+Return), raise EOFError. On *nix systems, readline is used if available.