python-输入

1. python2版本中

  • 咱们在银行ATM机器前取钱时,肯定需要输入密码,对不?
  • 那么怎样才能让程序知道咱们刚刚输入的是什么呢??
  • 大家应该知道了,如果要完成ATM机取钱这件事情,需要先从键盘中输入一个数据,然后用一个变量来保存,是不是很好理解啊
  • 1.1 raw_input()

    • 在Python中,获取键盘输入的数据的方法是采用 raw_input 函数(至于什么是函数,咱们以后的章节中讲解),那么这个 raw_input 怎么用呢?
    • 看如下示例:
    password = raw_input("请输入密码:")
    print '您刚刚输入的密码是:', password
    
    • 运行结果:
    • 注意:
      • raw_input()的小括号中放入的是,提示信息,用来在获取数据之前给用户的一个简单提示
      • raw_input()在从键盘获取了数据以后,会存放到等号右边的变量中
      • raw_input()会把用户输入的任何值都作为字符串来对待
  • 1.2 input()

    • input()函数与raw_input()类似,但其接受的输入必须是表达式。
    >>> a = input() 
    123
    >>> a
    123
    >>> type(a)
    <type 'int'>
    >>> a = input()
    abc
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "<string>", line 1, in <module>
    NameError: name 'abc' is not defined
    >>> a = input()
    "abc"
    >>> a
    'abc'
    >>> type(a)
    <type 'str'>
    >>> a = input()
    1+3
    >>> a
    4
    >>> a = input()
    "abc"+"def"
    >>> a
    'abcdef'
    >>> value = 100
    >>> a = input()
    value
    >>> a
    100
    
    • input()接受表达式输入,并把表达式的结果赋值给等号左边的变量

2. python3版本中

  • 没有raw_input()函数,只有input()
  • 并且 python3中的input与python2中的raw_input()功能一样
posted @ 2018-12-26 23:43  CH520  阅读(157)  评论(0编辑  收藏  举报