python中,用input()输入一个整数

  • Post author:
  • Post category:python


我想用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.



版权声明:本文为xiaotao_1原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。