码迷,mamicode.com
首页 > 编程语言 > 详细

python基础4 用户交互

时间:2017-07-20 10:09:23      阅读:132      评论:0      收藏:0      [点我收藏+]

标签:学习   const   raw_input   自己   err   表达式   env   接受   get   

本节主要内容:

  • 例一 input()
  • 例二字符拼接
  • 例三 %占位符
  • 例四 raw_input()和 input()
  • 例五 格式化用户交互
  • 例六 数组格式化
  • 参考网页

用户使用input函数实现交互,本节通过示例来学习此节内容:

例一 input()

#!/usr/bin/env python 
# -*- coding:utf-8 -*-
# Author:Cathy Wu

username =input("username")
password =input("password")
print(username,password)

运行后,提示输入“username”和“password”, 输入后打印“username”和“password”。

例二字符拼接

name = input("name")
age = input("age")
salary = input("salary")
info = ‘‘‘
------info  of ‘‘‘ + name+‘‘‘---
age:‘‘‘ + age + ‘‘‘
salary:‘‘‘+ salary
print(info)

运行结果:

namecathy
age25
salary5000

------info  of cathy---
age:25
salary:5000

例三 %占位符

%s或者%d %对应一个占位符号,要一一对应。s代表string,d代表数字,%f代表浮点数。默认所有的输入都是string。

name = input("name")
age = input("age")
salary = input("salary")
info =‘‘‘---info of %s ----
name: %s
age:%s
salary: %s
‘‘‘ % (name,name,age,salary)
print(info)

运行结果:

namecathy
age12
salary12
---info of cathy ----
name: cathy
age:12
salary: 12

例四 raw_input() 和 input()

python3里面没有raw_input. raw_input在python2里面有使用, input() 本质上还是使用 raw_input() 来实现的,只是调用完 raw_input() 之后再调用 eval() 函数。

>>> raw_input_A = raw_input("raw_input: ")
raw_input: abc
 >>> input_A = input("Input: ")
Input: abc

Traceback (most recent call last):
  File "<pyshell#1>", line 1, in <module>
    input_A = input("Input: ")
  File "<string>", line 1, in <module>
NameError: name ‘abc‘ is not defined
 >>> input_A = input("Input: ")
Input: "abc"
 >>>

上面的代码可以看到:这两个函数均能接收 字符串 ,但 raw_input() 直接读取控制台的输入(任何类型的输入它都可以接收)。而对于 input() ,它希望能够读取一个合法的 python 表达式,即你输入字符串的时候必须使用引号将它括起来,否则它会引发一个 SyntaxError 。

>>> raw_input_B = raw_input("raw_input: ")
raw_input: 123
 >>> type(raw_input_B)
 <type ‘str‘>
>>> input_B = input("input: ")
input: 123
>>> type(input_B)
<type ‘int‘>
>>>

上面的代码可以看到:raw_input() 将所有输入作为字符串看待,返回字符串类型。而 input() 在对待纯数字输入时具有自己的特性,它返回所输入的数字的类型( int, float );同时在前一段代码 知道,input() 可接受合法的 python 表达式,举例:input( 1 + 3 ) 会返回 int 型的 4 。

例五 格式化用户交互

官方建议的用户使用方法。

name = input("name")
age = input("age")
salary = input("salary")
info3 = ‘‘‘
---info3 of {_name} ----
name: {_name}
age: {_age}
salary: {_salary}
‘‘‘.format(_name=name,
           _age=age,
           _salary=salary)
print(info3)

运行结果如下:

namecathy
age25
salary10000

---info3 of cathy ----
name: cathy
age: 25
salary: 10000

例六 数组格式化

name = input("name")
age = input("age")
salary = input("salary")
info4 = ‘‘‘
---info4 of {0} ----
name: {0}
age: {1}
salary: {2}
‘‘‘.format(name, age, salary)
print(info4)

运行结果如下:

namecathy
age25
salary15000

---info4 of cathy ----
name: cathy
age: 25
salary: 15000

其他

上诉例子,密码是可见的,怎么让密码不可见了,有个模块getpass

import getpass

username = input("username:")
password = getpass.getpass("password")

print(username, password)

参考网页

http://www.cnblogs.com/way_testlife/archive/2011/03/29/1999283.html

python基础4 用户交互

标签:学习   const   raw_input   自己   err   表达式   env   接受   get   

原文地址:http://www.cnblogs.com/cathywu/p/7209128.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!