笨办法学Python - 习题11-12: Asking Questions & Prompting People
目录
1、习题 11: 提问
学习目标:了解人机交互场景,熟悉raw_input 的用法。
1、在 python2.x 中 raw_input( ) 和 input( ),两个函数都存在,具体区别详情请参考,其中区别为:
- raw_input( ) 将所有输入作为字符串看待,返回字符串类型。
- input( ) 只能接收“数字”的输入,在对待纯数字输入时具有自己的特性,它返回所输入的数字的类型( int, float )。
2、在 python3.x 中 raw_input( )和 input( ) 进行了整合,去除了 raw_input( ),仅保留了 input( ) 函数,其接收任意任性输入,将所有输入默认为字符串处理,并返回字符串类型。
习题十一中的练习代码是:
#! -*-coding=utf-8-*- print("how old are you?"), age = raw_input() print "how tall are you?", height = raw_input() print "how much do you weigh?", weight = raw_input() print "so, you're %r old, %r tall and %r heavy." % ( age, height, weight)
c:\python27\python.exe d:/pythoncode/stupid_way_study/demo11/exer11-1.py how old are you? 14 how tall are you? 34 how much do you weigh? 123 so, you're '14' old, '34' tall and '123' heavy. process finished with exit code 0
上述代码有一个问题,就是每一个print 一句后面都有一个逗号,这是因为这样的话print 就不会输出新行符而结束这一行跑到下一行去了,具体详情用法请参考。
2、习题 12: 提示别人
学习目标:继续学习raw_input 的用法,了解怎么添加提示信息。
习题十二中的练习代码是:
age = raw_input("how old are you? ") height = raw_input("how tall are you? ") weight = raw_input("how much do you weigh? ") print "so, you're %r old, %r tall and %r heavy." % (age, height, weight)
上述代码的运行结果为:
c:\python27\python.exe d:/pythoncode/stupid_way_study/demo11/exer11-1.py how old are you? 23 how tall are you? 45 how much do you weigh? 123 so, you're '23' old, '45' tall and '123' heavy. process finished with exit code 0
了解一下pydoc的用法:
d:\pythoncode\stupid_way_study\demo11>python -m pydoc exer11-1 how old are you? 12 how tall are you? 23 how much do you weigh? 123 so, you're '12' old, '23' tall and '123' heavy. help on module exer11-1: # 展示该文件具体内容 name exer11-1 file d:\pythoncode\stupid_way_study\demo11\exer11-1.py description # print("how old are you?"), # age = raw_input() # # print "how tall are you?", # height = raw_input() # # print "how much do you weigh?", # weight = raw_input() # # print "so, you're %r old, %r tall and %r heavy." % ( # age, height, weight) data age = '12' height = '23' weight = '123'
pydoc是python自带的一个文档生成工具,使用pydoc可以很方便的查看类和方法结构
同时我们可以在本地开启端口,然后在浏览器端看源代码解释:
本地窗口使用命令python3 -m pydoc -p 1234 开启:
浏览器端可直接访问开放的端口:
当然还可以在命令行下直接查看某一模块的源代码:
使用命令:python3 -m pydoc os
3、总结
这两题主要是学习了和计算机交互的函数raw_input() ,注意由于python2中input() 自身的原因会导致一些问题,所以一般在python2中只使用input(),然后学习了解pydoc的用法。
上一篇: Laravel 数据库操作 Eloquent ORM
下一篇: 同数据源不同数据库查询