1.3.4 人机对话基本接口

  所谓对话,最简单的形式就是你问我答,或者我问你答。而人机对话或人机交互最基本的功能应该是机器能够接受用户的输入,并且能够把处理结果通过一定的形式展示给用户。在Python中,内置函数 input() 用来接受用书输入,print() 则用于把处理结果或其他信息展示给用户。对于 input() 而言,不论用户输入什么内容,一律作为字符串对待,必要的时候可以使用内置函数 eval() 对用户输入的内容进行类型转换。例如:

 

 1 >>> x = input('Please input:')
 2 Please input:111
 3 >>> x
 4 '111'
 5 >>> type(x)        #把用户的输入作为字符串对待
 6 <class 'str'>
 7 >>> 
 8 >>> int(x)
 9 111
10 >>> 
11 >>> x
12 '111'
13 >>> 
14 >>> eval(x)        #对字符串求值
15 111
16 >>> 
17 
18 >>> x = input('Please input:')      
19 Please input:'hello world'
20 >>> x
21 "'hello world'"
22 >>> 
23 >>> eval(x)        #如果本来就像输入字符串,就不用再输入引号了
24 'hello world'
25 >>> 
26 
27 
28 
29 内置函数 print(value,...,sep=' ',end='\',file=sys.stdout,flush=False)
30 其中,sep参数之前为要输出的内容(可以有多个);sep参数用于指定数据之间的分隔符,默认为空格;file参数用于指定输出位置,默认为标准控制台,也可以重定向输出到文件。例如:
31 
32 >>> print(1,3,5,7,sep='\t')    #修改默认分隔符
33 1    3    5    7
34 >>> 
35 >>> for i in range(10):        #修改默认行尾结束符,不换行
36     print(i,end = ' ')
37 
38     
39 0 1 2 3 4 5 6 7 8 9 
40 >>> 
41 >>> fp = open(r'C:\Users\dddd\Desktop\a.txt','a')
42 >>> print('Hello World!',file=fp)        #重定向输出内容到文件
43 >>> fp.close()                           #要显式关闭一下文件连接
44 >>> 
45 
46 #如果不想手动关闭文件连接可以用with语法
47 >>> with open(r'C:\Users\dddd\Desktop\a.txt','a') as fp:
48     print('I Love China !',file=fp)
49 
50     
51 >>> #等程序执行完,在打开a.txt文件,就发现,'I Love China !' 已经写入到文件中了。很方便吧。

 

  拓展知识:

    Python标准库sys还提供了 read() 和 readline()方法用来从键盘接受指定数量的字符。例如:

 1 >>> import sys
 2 >>> x = sys.stdin.read(5)    #读取 5 个字符,如果输入不足 5 个,等待继续输入
 3 abc
 4 d
 5 >>> x
 6 'abc\nd'
 7 >>> 
 8 >>> x = sys.stdin.read(5)    #读取5个字符,如果超出5个,截断
 9 abcdefghjklmnopqrstuvwxyz
10 >>> x
11 '\nabcd'
12 >>> 
13 >>> 
14 >>> x = sys.stdin.read(5)    #从缓冲区内继续读取 5 个字符
15 >>> x
16 'efghj'
17 >>> 
18 >>>  x = sys.stdin.read(5)
19  
20 SyntaxError: unexpected indent
21 >>> 
22 >>> x = sys.stdin.read(5)
23 >>> x
24 'klmno'
25 >>> 
26 >>> x = sys.stdin.read(5)
27 >>> x
28 'pqrst'
29 >>> x = sys.stdin.read(5)
30 >>> x
31 'uvwxy'
32 >>> 
33 >>> x = sys.stdin.read(5)    #缓冲区内不足 5 个字符,就等待用户继续输入
34 1234567
35 >>> x
36 'z\n123'
37 >>> 
38 >>>  
39 >>> x = sys.stdin.readline()   #从缓冲区内读取字符,遇到换行符就结束
40 >>> x
41 '4567\n'
42 >>> #就是如果不指定每次读取的字符数,以行为单位,行是以换行符结束的,遇到一个换行符就表示一行结束
43 >>> 
44 >>> x = sys.stdin.readline()
45 abcd
46 >>> x
47 'abcd\n'
48 >>> 
49 >>> x = sys.stdin.readline(13)   #如果缓冲区内容比需要的少,就遇到换行符结束
50 abcdefg
51 >>> x
52 'abcdefg\n'
53 >>> 
54 >>> x = sys.stdin.readline(13)   #如果缓冲区内容必须要的多,就截断
55 abcdefghijklmnopq
56 >>> x
57 'abcdefghijklm'
58 >>> 
59 >>> x = sys.stdin.readline(13)   #从缓冲区继续读取
60 >>> x
61 'nopq\n'
62 >>> 

    

    Python标准库 pprint 还提供了更加友好的输出函数 pprint() ,可以更好地控制输出根式,如果要输出的内容多余一行则会自动添加换行和缩进来更好地展示内容的结构。例如:

 1 >>> import pprint
 2 >>> 
 3 >>> t = [[[['black','cyan'],'white',['green','red']],[['magenta','yellow'],'blue']]]
 4 >>> 
 5 >>> pprint.pprint(t)   #默认 width = 80
 6 [[[['black', 'cyan'], 'white', ['green', 'red']],
 7   [['magenta', 'yellow'], 'blue']]]
 8 >>> 
 9 >>> pprint.pprint(t,width=50)
10 [[[['black', 'cyan'], 'white', ['green', 'red']],
11   [['magenta', 'yellow'], 'blue']]]
12 >>> 
13 >>> 
14 >>> pprint.pprint(t,width=30)     #根据需要进行换行和缩进
15 [[[['black', 'cyan'],
16    'white',
17    ['green', 'red']],
18   [['magenta', 'yellow'],
19    'blue']]]
20 >>> 

 

posted @ 2018-03-06 10:49  Avention  阅读(659)  评论(0编辑  收藏  举报