input() 是 Python 的内嵌函数,用以从控制面板载入用户输入的内容 。input() 函数一直以字符串数组的方式来解决用户输入的内容 ,因此 用户输入的内容能够包括一切标识符。

input() 是 Python 的内嵌函数,用以从控制面板载入用户输入的内容。input() 函数一直以字符串数组的方式来解决用户输入的内容,因此 用户输入的内容能够包括一切标识符 。

input() 函数的使用方法为:

str = input(tipmsg)

表明:

  • str 表明一个字符串类型的自变量 ,input 会将载入到的字符串数组放进 str 中。
  • tipmsg 表明信息提示,它会显示信息在操纵台子上,告知用户应当输入哪些的内容;如果不写 tipmsg ,就不容易有一切信息提示。


【案例】input() 函数的简易应用:

a = input("Enter a number: ") b = input("Enter another number: ") print("aType: ", type(a)) print("bType: ", type(b)) result = a b print("resultValue: ", result) print("resultType: ", type(result))

运作結果实例:

Enter a number: 100↙
Enter another number: 45↙
aType: 
bType: 
resultValue:  10045
resultType: 

表明按住回车 ,按住回车后 input() 载入就结束了 。

本例中大家输入了2个整数金额,期待测算出他们的和,可是不如人意 ,Python 仅仅他们当做了字符串数组, 具有了拼凑字符串数组的功效,而不是求饶的功效 。

我们可以应用 Python 内嵌函数将字符串数组转化成要想的种类 ,例如:

  • int(string) 将字符串数组转化成 int 种类;
  • float(string) 将字符串数组转化成 float 种类;
  • bool(string) 将字符串数组转化成 bool 种类。


改动上边的编码,将用户输入的内容转化成数据:

a = input("Enter a number: ") b = input("Enter another number: ") a = float(a) b = int(b) print("aType: ", type(a)) print("bType: ", type(b)) result = a b print("resultValue: ", result) print("resultType: ", type(result))

运作結果:

Enter a number: 12.5↙
Enter another number: 64↙
aType: 
bType: 
resultValue:  76.5
resultType: 

有关 Python 2.x

上边解读的是 Python 3.x 中 input() 的使用方法,可是在较老的 Python 2.x 中状况就不一样了。Python 2.x 共出示了2个输入函数 ,分别是 input() 和 raw_input():

  • Python 2.x raw_input() 和 Python 3.x input() 实际效果是一样的,都只有以字符串数组的方式载入用户输入的内容 。
  • Python 2.x input() 看上去有点儿怪异,它规定用户输入的内容务必合乎 Python 的英语的语法 ,稍有粗心大意便会失败,一般而言只有是整数金额、小数 、复数、字符串数组等。


较为逼迫的是,Python 2.x input() 规定用户在输入字符串数组时务必应用冒号包围着 ,这有悖 Python 简易实用的标准 ,因此 Python 3.x 取消了这类输入方法。

改动这节第一段编码,除掉 print 后边的括弧:

a = input("Enter a number: ") b = input("Enter another number: ") print "aType: ", type(a) print "bType: ", type(b) result = a b print "resultValue: ", result print "resultType: ", type(result)

在 Python 2.x 下运作该编码:

Enter a number: 45↙
Enter another number: 100↙
aType: 
bType: 
resultValue:  145
resultType: 

末尾褔利

这儿免费送大伙儿一套2020全新python新手入门到高級新项目实战演练教程视频,点一下这儿就可以领到!或是添加时尚博主Python沟通交流圈(QQ群:1049600759)一键下载 。


 

文中的文本及图片来自互联网再加上自身的念头,仅作学习培训、沟通交流应用,不具备一切商业行为,著作权归创作者全部,如有什么问题请立即在线留言以作解决。

文章来源于网络 ,如有侵权请联系站长QQ61910465删除
本文版权归趣快排营销www.seoguRubloG.com 所有,如有转发请注明来出,竞价开户托管,seo优化请联系✚Qq61910465