python学习七--用户交互和格式化输出
2017-12-25 22:45
676 查看
raw_input 直接读取控制台的输入(任何类型的输入它都可以接收),raw_input() 将所有输入作为字符串看待,返回字符串类型
input 输入的什么类型,不转换,它希望能够读取一个合法的 python 表达式,即你输入字符串的时候必须使用引号将它括起来,
否则它会引发一个 SyntaxError 。而 input() 在对待纯数字输入时具有自己的特性,它返回所输入的数字的类型( int, float )
s% 字符串
d% 十进制整数 必须写整数
f% 浮点数
[root@node01 python]# vi ex03.py
#!/usr/bin/env python
#_*_ coding:utf-8 _*_
name = raw_input('pls input your name:')
age = raw_input('pls input your age:')
print name, age
[root@node01 python]# python ex03.py
pls input your name:wolf
pls input your age:1
wolf 1
代入变量
[root@node01 python]# name=wolf
[root@node01 python]# echo "my name is $name,haha"
my name is wolf,haha
[root@node01 python]# vi ex03.py
------------------------
-----------------------
#!/usr/bin/env python
#_*_ coding:utf-8 _*_
name = raw_input('pls input your name:')
age = raw_input('pls input your age:')
job = raw_input('pls input your jor:')
salary = raw_input('Salary:')
print '''
Personal information of %s:
Name: %s
Age : %s
Job : %s
Salay:%s
-----------------------
''' % (name,name, age,job,salary)
[root@node01 python]# python ex03.py
pls input your name:wolf
pls input your age:2
pls input your jor:z
Salary:d
Personal information of wolf:
Name: wolf
Age : 2
Job : z
Salay:d
-----------------------
[root@node01 python]# vi ex03.py
#!/usr/bin/env python
#_*_ coding:utf-8 _*_
name = raw_input('pls input your name:')
age = raw_input('pls input your age:')
job = raw_input('pls input your jor:')
salary = raw_input('Salary:')
print type(age)
print '''
Personal information of %s:
Name: %s
Age : %s
Job : %s
Salay:%s
-----------------------
''' % (name,name, age,job,salary)
===========================~
"ex03.py" 19L, 355C written
[root@node01 python]# python ex03.py
pls input your name:df
pls input your age:2
pls input your jor:df
Salary:80080
<type 'str'> 字符串
Personal information of df:
Name: df
Age : 2
Job : df
Salay:80080
-----------------------
===========================
[root@node01 python]# vi ex03.py
#!/usr/bin/env python
#_*_ coding:utf-8 _*_
name = raw_input('pls input your name:')
age = int(raw_input('pls input your age:'))
job = input('pls input your jor:') 输入什么格式当什么格式来处理
salary = raw_input('Salary:')
print type(age)
print '''
Personal information of %s:
Name: %s
Age : %d 这里必须要整数,不然会报错
Job : %s 什么类型来都可以,自动转成字符串
Salay:%s
-----------------------
''' % (name,name, age,job,salary)
=====================
[root@node01 python]# python ex03.py
pls input your name:wolf
pls input your age:2
pls input your jor:'2'
Salary:df
<type 'int'>
Personal information of wolf:
Name: wolf
Age : 2
Job : 2
Salay:df
-----------------------
[root@node01 python]# python ex03.py
pls input your name:wolf
pls input your age:2
pls input your jor:'d'
Salary:fd
<type 'int'>
Personal information of wolf:
Name: wolf
Age : 2
Job : d
Salay:fd
-----------------------
[root@node01 python]# cat ex03.py
#!/usr/bin/env python
#_*_ coding:utf-8 _*_
name = raw_input('pls input your name:')
age = int(raw_input('pls input your age:'))
job = input('pls input your jor:')
salary = raw_input('Salary:')
print type(age)
print '''
Personal information of %s:
Name: %s
Age : %d
Job : %s
Salay:%s
-----------------------
''' % (name,name, age,job,salary)
[root@node01 python]# python ex03.py
pls input your name:wolf
pls input your age:2
pls input your jor:2.2
Salary:90
<type 'int'>
Personal information of wolf:
Name: wolf
Age : 2
Job : 2.2
Salay:90
-----------------------
[root@node01 python]#
input 输入的什么类型,不转换,它希望能够读取一个合法的 python 表达式,即你输入字符串的时候必须使用引号将它括起来,
否则它会引发一个 SyntaxError 。而 input() 在对待纯数字输入时具有自己的特性,它返回所输入的数字的类型( int, float )
s% 字符串
d% 十进制整数 必须写整数
f% 浮点数
[root@node01 python]# vi ex03.py
#!/usr/bin/env python
#_*_ coding:utf-8 _*_
name = raw_input('pls input your name:')
age = raw_input('pls input your age:')
print name, age
[root@node01 python]# python ex03.py
pls input your name:wolf
pls input your age:1
wolf 1
代入变量
[root@node01 python]# name=wolf
[root@node01 python]# echo "my name is $name,haha"
my name is wolf,haha
[root@node01 python]# vi ex03.py
------------------------
-----------------------
#!/usr/bin/env python
#_*_ coding:utf-8 _*_
name = raw_input('pls input your name:')
age = raw_input('pls input your age:')
job = raw_input('pls input your jor:')
salary = raw_input('Salary:')
print '''
Personal information of %s:
Name: %s
Age : %s
Job : %s
Salay:%s
-----------------------
''' % (name,name, age,job,salary)
[root@node01 python]# python ex03.py
pls input your name:wolf
pls input your age:2
pls input your jor:z
Salary:d
Personal information of wolf:
Name: wolf
Age : 2
Job : z
Salay:d
-----------------------
[root@node01 python]# vi ex03.py
#!/usr/bin/env python
#_*_ coding:utf-8 _*_
name = raw_input('pls input your name:')
age = raw_input('pls input your age:')
job = raw_input('pls input your jor:')
salary = raw_input('Salary:')
print type(age)
print '''
Personal information of %s:
Name: %s
Age : %s
Job : %s
Salay:%s
-----------------------
''' % (name,name, age,job,salary)
===========================~
"ex03.py" 19L, 355C written
[root@node01 python]# python ex03.py
pls input your name:df
pls input your age:2
pls input your jor:df
Salary:80080
<type 'str'> 字符串
Personal information of df:
Name: df
Age : 2
Job : df
Salay:80080
-----------------------
===========================
[root@node01 python]# vi ex03.py
#!/usr/bin/env python
#_*_ coding:utf-8 _*_
name = raw_input('pls input your name:')
age = int(raw_input('pls input your age:'))
job = input('pls input your jor:') 输入什么格式当什么格式来处理
salary = raw_input('Salary:')
print type(age)
print '''
Personal information of %s:
Name: %s
Age : %d 这里必须要整数,不然会报错
Job : %s 什么类型来都可以,自动转成字符串
Salay:%s
-----------------------
''' % (name,name, age,job,salary)
=====================
[root@node01 python]# python ex03.py
pls input your name:wolf
pls input your age:2
pls input your jor:'2'
Salary:df
<type 'int'>
Personal information of wolf:
Name: wolf
Age : 2
Job : 2
Salay:df
-----------------------
[root@node01 python]# python ex03.py
pls input your name:wolf
pls input your age:2
pls input your jor:'d'
Salary:fd
<type 'int'>
Personal information of wolf:
Name: wolf
Age : 2
Job : d
Salay:fd
-----------------------
[root@node01 python]# cat ex03.py
#!/usr/bin/env python
#_*_ coding:utf-8 _*_
name = raw_input('pls input your name:')
age = int(raw_input('pls input your age:'))
job = input('pls input your jor:')
salary = raw_input('Salary:')
print type(age)
print '''
Personal information of %s:
Name: %s
Age : %d
Job : %s
Salay:%s
-----------------------
''' % (name,name, age,job,salary)
[root@node01 python]# python ex03.py
pls input your name:wolf
pls input your age:2
pls input your jor:2.2
Salary:90
<type 'int'>
Personal information of wolf:
Name: wolf
Age : 2
Job : 2.2
Salay:90
-----------------------
[root@node01 python]#
相关文章推荐
- python用户交互、格式化输出
- python之模块的导入和用户的交互格式化输出
- python学习03--格式化输出
- 用户交互和格式化输出
- 编程语言的学习--------python3 列表与原则的嵌套以及格式化输出
- Python 学习入门(8)—— 格式化输出
- Python 学习入门(8)—— 格式化输出
- 1.30继续python学习 运算符 while for 格式化输出
- python格式化输出 学习学习
- python格式化输出学习
- Python_3_学习之路_(1)变量_用户交互_if_else___while__for
- Python学习-格式化输出(format)
- python学习习题总结(8)——简单选择排序,列表解析式使用(格式化输出杨辉三角,九九乘法表)
- 工作目录 python格式化字符串 logging不输出 linux其他用户执行权限 2016.08.19回顾
- Python学习入门基础教程(learning Python)--1.2.2 Python格式化输出基础 .
- Python_3_学习之路_(1)变量_用户交互_if_else___while__for
- python 3 用户输入和格式化输出
- Python学习-格式化输出
- Python学习入门基础教程(learning Python)--1.2.3 Python格式化输出百分比
- python学习笔记10(用户交互程序)