• 技术文章 >常见问题 >Python常见问题

    python里input怎么解释

    silencementsilencement2019-10-10 15:21:51原创11204

    python中input函数有类似c中的scanf函数的功能。

    Python2中input使用如下:

    >>>x = input("x:")
    x: 3
    >>>y = input("y:" )
    y: 4
    >>> print x*y
    12

    但是Python3中input使用会有如下的提示:

    >>> x = input("x:")
    x:3
    >>> y = input("y:")
    y:4
    >>> print (x*y)
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    TypeError: can't multiply sequence by non-int of type 'str'
    >>>

    原因:
    Python3以后的版本中,raw_input和input合体了,取消了raw_input,并用input代替,所以说现在版本的input接受的是字符串,可以如下处理:

    >>> x = int(input("x:"))
    x:3
    >>> y = int(input("y:"))
    y:4
    >>> print (x*y)
    12

    更多学习内容,请点击Python学习网

    专题推荐:input
    品易云
    上一篇:python如何导入re模块 下一篇:python函数定义如何使用

    相关文章推荐

    • Python fileinput模块:逐行读取多个文件• Python input()函数:获取用户输入的字符串• python3下的input函数怎么用

    全部评论我要评论

    © 2021 Python学习网 苏ICP备2021003149号-1

  • 取消发布评论
  • 

    Python学习网