首页 文章资讯内容详情

python 中的input

2026-06-01 4 花语

本文内容纲要:

渣渣之路。

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

#-*-coding:utf-8-*- defdisplay(message): printmessage defgive_me_five(): five=5 returnfive defask_yes_no(question): """ Askayesornoquestions. """ response=None whileresponsenotin(y,n): response=input(question).lower() returnresponse display("hereisamessageforyou\n") number=give_me_five() print"HereswhatIgotfromgive_me_five():",number answer=ask_yes_no("\nPleaseenteryorn:") print"Thankyouforentering:",answer

发现自己在pycharm下输入的:y会报错

Pleaseenteryorn:y

Traceback(mostrecentcalllast):

File"E:/Project/actneed411/furion/static/js/template/testa.py",line25,in

answer=ask_yes_no("\nPleaseenteryorn:")

File"E:/Project/actneed411/furion/static/js/template/testa.py",line19,inask_yes_no

response=input(question).lower()

File"",line1,in

NameError:nameyisnotdefined

但是,输入:y或者"y"却是对的:

hereisamessageforyou

HereswhatIgotfromgive_me_five():5

Pleaseenteryorn:y"y"

Thankyouforentering:y

二、探究python中的input【1】

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

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

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输入函数input2-----

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

本文内容总结:

原文链接:https://www.cnblogs.com/mxh1099/p/5412299.html