zoukankan      html  css  js  c++  java
  • python中raw_input() 与 input()

    参考网址:http://www.cnblogs.com/way_testlife/archive/2011/03/29/1999283.html

    在python中如何接收一个输入的字符串。

    举个例子:

    #coding=utf-8           
    #测试input 和 raw_input 
    x = input("please input :")
    print x

    运行:python 23.py

    输入一个数字

    please input :9
    9

    输入一个字符串

    please input :aaa
    Traceback (most recent call last):
      File "23.py", line 3, in <module>
        x = input("please input :")
      File "<string>", line 1, in <module>
    NameError: name 'aaa' is not defined

     看到了吧,如果直接输入字符,会报错。那怎么解决这个问题呢?

    需要把input 改成raw_input,修改后的代码是这样:

    #coding=utf-8
    #测试input 和 raw_input
    x = raw_input("please input :")                                                                                           
    print x 

    运行结果:

    please input :aa
    aa

    input([prompt])

        Equivalent to eval(raw_input(prompt)) 

    input() 本质上还是使用 raw_input() 来实现的,只是调用完 raw_input() 之后再调用 eval() 函数,所以,你甚至可以将表达式作为 input() 的参数,并且它会计算表达式的值并返回它。

    不过在 Built-in Functions 里有一句话是这样写的:Consider using the raw_input() function for general input from users.

    除非对 input() 有特别需要,否则一般情况下我们都是推荐使用 raw_input() 来与用户交互。

    raw_input() 将所有输入作为字符串看待,返回字符串类型。而 input() 在对待纯数字输入时具有自己的特性,它返回所输入的数字的类型( int, float )

  • 相关阅读:
    JAVA之各种jar包
    JAVA学习之路 swagger
    IDEA插件之实用插件
    华为云服务器 Centos7.8 安装Mysql8
    .Net Core之设计模式练习
    基于IdentityServer4实现单点登录
    .Net Core Web即时通讯之SignalR
    mysql 优化
    SpringMvc拦截器
    Java 枚举类
  • 原文地址:https://www.cnblogs.com/wangkongming/p/4651931.html
Copyright © 2011-2022 走看看