python 中的input

  渣渣之路。html

1、 在python编程初学者指南中的第六章、使用参数和返回值的例子中:python

# -*- coding: utf-8 -*-


def display(message):
print message


def give_me_five():
five = 5
return five


def ask_yes_no(question):
"""
Ask a yes or no questions.
"""
response = None
while response not in ('y', 'n'):
response = input(question).lower()
return response

display("here is a message for you\n")
number = give_me_five()
print "Here's what I got from give_me_five():", number
answer = ask_yes_no("\nPlease enter 'y' or 'n': ")
print "Thank you for entering:", answer

发现本身在pycharm下输入的:y会报错编程

Please enter 'y' or 'n': y
Traceback (most recent call last):
File "E:/Project/actneed411/furion/static/js/template/testa.py", line 25, in <module>
answer = ask_yes_no("\nPlease enter 'y' or 'n': ")
File "E:/Project/actneed411/furion/static/js/template/testa.py", line 19, in ask_yes_no
response = input(question).lower()
File "<string>", line 1, in <module>
NameError: name 'y' is not defined安全

可是,输入:'y'或者"y"倒是对的:  python2.7

here is a message for you函数

Here's what I got from give_me_five(): 5spa

Please enter 'y' or 'n': 'y' "y"
Thank you for entering: y .net

 

2、探究python中的input【1】htm

   由【1】中的文档中,python2.7中输入函数有两种:blog

      一、raw_input():返回的是字符串--string类型,即输入:1+2,返回显示的是:"1+2"

      二、input():返回的是数值类型,int,float等,即输入:1+2,返回显示的是:3

  而在python3中输入只有一种:

    input():返回的是字符串--string类型,没有数值类型了至关于原来的raw_input()

    【2】之前有分raw_input和input, raw_input读什么东西都是string, input会解析数据,

    版本3合并了raw_input和input, 只能读到string了, 原先的可解析版本不安全,

    若是要读到数值,使用类型转换:

      a = int(input("a="))

  刚好数中使用的是python是python3,这样就能解释通上边的问题了。

 

 

 

------------420 三--

   参考连接:【1】、python输入函数input 2-----

        【2】、python3中的input raw_input()变成了input()

相关文章
相关标签/搜索