zoukankan      html  css  js  c++  java
  • python input函数

    python input函数

    觉得有用的话,欢迎一起讨论相互学习~


    我的微博我的github我的B站

    • 对于python的input函数需要从python2和python3两方面讲。
    • 对于python3,通过input函数输入的所有内容都会作为str类型的字符串变量传入,只需要使用int和float进行强制类型转换就可以。
    # python3
    d=float(input('Please enter what is your initial balance: 
    '))
    p=float(input('Please input what is the interest rate (as a number): 
    '))
    Please enter what is your initial balance:
    50000
    Please input what is the interest rate (as a number):
    2.3
    
    • 对于python2,input函数可以识别输入的是int还是float,但是String类型的一定要""引号引起来,否则会出现错误。
    # python2
    >>>a = input("input:")
    input:123                  # 输入整数
    >>> type(a)
    <type 'int'>               # 整型
    >>> a = input("input:")    
    input:"runoob"           # 正确,字符串表达式
    >>> type(a)
    <type 'str'>             # 字符串
    >>> a = input("input:")
    input:runoob               # 报错,不是表达式,输入的runoob必须用括号括起来
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "<string>", line 1, in <module>
    NameError: name 'runoob' is not defined
    <type 'str'>
    
    • python2中的raw_input() 函数和python3中的input函数是一样的使用方法,将命令行的输入都会视为string类型。
    # python2
    >>>a = raw_input("input:")
    input:123
    >>> type(a)
    <type 'str'>              # 字符串
    >>> a = raw_input("input:")
    input:runoob
    >>> type(a)
    <type 'str'>              # 字符串
    >>>
    
  • 相关阅读:
    用 pytube 爬取 youtube 视频
    Ubuntu W: GPG error: http://archive.ubuntukey....NO_PUBKEY 8D5A09
    windows 下 putty 登陆服务器 显示matlab图形界面
    python27 windows 下三种安装第三方库的办法
    MCMC: The Metropolis-Hastings Sampler
    MCMC: The Metropolis Sampler
    Latex 数学符号
    差点被坑
    新年新flag
    听报告为什么要问问题
  • 原文地址:https://www.cnblogs.com/cloud-ken/p/12636769.html
Copyright © 2011-2022 走看看