Python与用户交互

如何交互

我们使用input()方法来与用户交互,但是无论我们输入的值是数字类型、字符串类型、列表类型,input的接收值都是字符串类型。

name = input('请输入你的名字:')
print(type(name))
请输入你的名字:rayn
<class 'str'>

Python2 和Python3 的交互(了解)

Python2中的raw_input()和Python3中的input()是一样的

但是Python2中的input,用户需要知道自己输入的数据是什么数据类型,否则就会报错。python3会自动强制转换。

``>>> age=input('请输入你的年龄:') ##python2中的input命令

请输入你的年龄:rad

Traceback (most recent call last):

File "", line 1, in

File "", line 1, in

NameError: name 'rad' is not defined



​```python
age=input('请输入你的年龄') ##Python3中的input命令
请输入你的年龄rad

格式化输出(考试必考)

占位符 (%s和%d)%d可以用做int类型

name = 'rayn'
height = 170
age = 20
print('my name is',name,'my height is',height,'my age is',age )
print('my name is %s my height is %s my age is %s'%(name,height,age))
my name is rayn my height is 170 my age is 20
my name is rayn my height is 170 my age is 20

format格式化 (了解)

name = input('name:>>>')
age = input('age:>>>')
height = input('height:>>>')
print('my name is {},my age is {},my height is{}'.format(name,age,height))
name:>>>rayn
age:>>>20
height:>>>180
my name is rayn,my age is 20,my height is180

f-string 格式化 (考试必考)

name = input('name:>>>')
age = input('age:>>>')
height = input('height:>>>')
print(f'my name is{name},my age is {age},my height is {height}')
name:>>>rayn
age:>>>20
height:>>>180
my name israyn,my age is 20,my height is 180
name = input('name:>>>')
age = input('age:>>>')
height = input('height:>>>')
print(f'my name is{name},my age is {int(age)+2},my height is {int(height)+3}') ##可以直接在大括号内进行算术运算
name:>>>rayn
age:>>>20
height:>>>180
my name israyn,my age is 22,my height is 183
print(f'my name is{name},my age is {int(age)+2},my height is {int(height)+3:.2f}') ##:.2f 标识保留2位小数点
my name israyn,my age is 22,my height is 183.00

最新文章

  1. 关于Unity3D自定义编辑器的学习
  2. android-配置文件AndroidManifest.xml
  3. 通过viewmodel找到view
  4. 通过javascript在网页端解压zip文件并查看压缩包内容
  5. 操蛋的CTex
  6. HDU 1312 Red and Black --- 入门搜索 DFS解法
  7. codeforce 611B New Year and Old Property
  8. linkin大话数据结构--Collections类
  9. 阿里云服务器 无法连接svn
  10. python递归
  11. STM32中断优先级彻底讲解
  12. 『TensorFlow』第三弹_可视化框架介绍_悄悄问圣僧
  13. Android网络请求与数据解析,使用Gson和GsonFormat解析复杂Json数据
  14. itcast-ssh-crm实践
  15. Insert Interval &amp; Merge Intervals
  16. JSP学习_02
  17. c++builder XE6 Remote Debuger 远程调试
  18. 微信公众号H5支付
  19. RT-thread内核之定时器管理
  20. 封装一个既能遍历数组又能遍历对象的的forEach函数

热门文章

  1. HDU4035(概率期望、树形、数学)
  2. python中的os.path.dirname与os.path.dirname(__file__)的用法
  3. SpirngMVC-JSON
  4. CI模板中如何引入模板
  5. Redis的发布和订阅
  6. shell脚本解析json文件
  7. BundleConfig包含js,css失败
  8. 导入动态Web项目到Eclipse中遇到的问题
  9. 搭建高可用mongodb集群—— 副本集
  10. MySql中查询语句实现分页功能