笨办法学Python:前17个练习回顾

闭上眼睛,程序浮现在你的脑海

这是写完初阶编程学习后的第一篇技术相关博客。本篇博客将会实践前面提到的方法,不借助任何外力,直接回顾知识点,通过程序来实现。然后再查漏补缺,有必要时再按照这个方法写一篇博客。

必要难度实践

知识点

已经学过的知识点足够丰富,主要有以下这些:

  • print方式打印输出
  • 各种格式化字符串
  • 标准输入、输出、打开文件、写入文件等
  • 自定义函数和函数传参数
  • 导入已有包等
  • 用户端的基本输入

设计程序实现

这段代码是自己没有看教程的基础上写的,中间出现过几次记不清语法通过官网搜索的过程。程序的目的是模仿前一段时间扎克伯格做的智能助手,做一个最简单的雏形,当然,功能还很不完善,最后还尝试了一下文件的读取过程。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
def greeting():
print "Hello,my name is Robot, what's your name,plz write it down below:"
name = raw_input('>')
if name != 'Leon':
print 'I see,so your name is %s' % name
elif name == 'Leon':
print 'Wow, your name is the same with my creater, %s' % name
print """
Since you bought me online, it is my honor to be your personal secret.
I will try my best to serve you well.
"""
return name

name = greeting()

# print name

def personal_information(name):
print "now let me know more about you, %s" %name
print "Which year were you born?",
birth_year = int(raw_input('>'))
print "what is your gender,male or female?",
gender = raw_input("M means male,F means female >")
age = 2016 - birth_year
# print """
#In general,you are %d year old (that is pretty young).
#And you are %s
#""" % (age,gender)

#personal_information(name)

def function_chose():
cal = "1. Caculation"
rdf = "2. Read files"
wtf = "3. Write files"
ors = "4. Others"
t = {1:cal,2:rdf,3:wtf,4:ors}
print """ As a robot,I can do a lot for you, here is the list
%s
%s
%s
%s

please give me the number you want.
""" %(t[1],rdf,wtf,ors)
function_chose = raw_input(">")
if int(function_chose) in range(1,5):
print t[int(function_chose)]
else:
print "Ops,I don't understand what you mean."
return function_chose

function_chose()
# print function_chose()

因为对文件的读入读出不是很自信,所以单独写了一个文件,最后的write函数没有成功。

1
2
3
4
5
6
7
8
9
10
11
12
13
import io

a = io.open("test.md")
print a.read()

b = """ lalalal
aaaa
bbbb
cccc
dddd
"""

a.write()

内容总结

查看Exercise 15: Reading FilesExercise 16: Reading and Writing Files,发现自己还有很多知识点遗忘了,在这里列出清单。在下一次的12小时之后再次尝试:

  • 在IDE运行程序时直接传递参数
  • 文件操作不需要单独导入包,实现有选择的打开、读取、写入、关闭

下一步行动

  • 梳理前17个练习的知识点,形成索引
  • 遗忘的知识点训练,通过程序来说明自己学会了

体会感受

以前写程序遇到不会的,第一反应是找文档,看案例,翻出原来的代码。这一次并没有,首先动用你最重要的器官:大脑,使劲的想,模模糊糊的记忆也没有关系,先尝试写出来,如果报错不断调试。不到”山穷水尽“不要看文档,千万不能去看原来的教程,没有任何意义。
基于“最小行动”的训练,看到跑出来的自己的想法,自信了许多。很小的进步告诉自己再也不会原地转圈了,也让自己知道了还有更大的差距,因为自己想实现的功能实在太多,要来慢慢一个一个的玩起来。

声明: 本文转载需标明出处,禁止用于商业目的。

ChangeLog

161227 新建