习题34:访问列表的元素
序数(ordinal number):从1开始编号,表示事物的顺序
基数(cardinal number):从0开始,表示你可以任意抓取元素
1. 上网搜索一下关于序数(ordinal number)和基数(cardinal number)的知识并阅读一下。
2. 以你对于这些不同的数字类型的了解,解释一下为什么 “January 1, 2010” 里是2010 而不是 2009?(提示:你不能随机挑选年份。)
年份是序数,从1开始,没有0年
3. 再写一些列表,用一样的方式作出索引,确认自己可以在两种数字之间互相翻译。
4. 使用 python 检查自己的答案。
animals = ['bear', 'python', 'peacock', 'kangaroo', 'whale', 'platypus']
# 熊、孔雀、袋鼠、鲸、鸭嘴兽
for i in range(0, 6):
print("The animal at %d is the %dst animal and is a %s" % (i, i+1, animals[i]))
习题35:分支和函数
from sys import exit
def gold_room():
print("The room is full of gold. How much do you take?")
choice = input(">")
if "0" in choice or "1" in choice:
how_much = int(choice)
else:
dead("Man, learn to type a number.")
if how_much < 50:
print("Nice, you're not greedy, you win!")
exit(0)
else:
dead("You greed bastard!")
def bear_room():
print("There is a bear here.")
print("The near has a bunch of honey.")
print("The fat bear is in front of another door.")
print("How are you going to moving the bear?")
bear_moved = False
while True:
choice = input(">")
if choice == "take honey":
dead("The bear looks at you then slaps your face off.")
elif choice == "taunt near" and not bear_moved:
print("The bear has moved from the door. You can go through it now.")
bear_moved = True
elif choice == "taunt near" and bear_moved:
dead("The bear gets pissed off and chews your leg off.")
elif choice == "open door" and bear_moved:
gold_room()
else:
print("I got no idea what that means.")
def cthulhu_room():
print("Here you see the great evil Cthulhu.")
print("He, it, whatever stares at you and you go insane.")
print("Do you flee for your life or eat your head?")
choice = input(">")
if "flee" in choice:
start()
elif "head" in choice:
dead("Well that was tasty!")
else:
cthulhu_room()
def dead(why):
print(why, "Good job!")
exit(0)
def start():
print("You are in a dark room.")
print("There is a door to your right and left.")
print("Which one do you take?")
choice = input(">")
if choice == "left":
bear_room()
elif choice == "right":
cthulhu_room()
else:
dead("You stumble around the room until you starve.")
start()
不同的结果
start下的三种选择
选right的可能结果
选left的可能结果太多
1. 把这个游戏的地图画出来,把自己的路线也画出来。
2. 改正你所有的错误,包括拼写错误。
3. 为你不懂的函数写注解。记得文档注解该怎么写吗?
4. 为游戏添加更多元素。通过怎样的方式可以简化并且扩展游戏的功能呢?
5. 这个 gold_room 游戏使用了奇怪的方式让你键入一个数字。这种方式会导致什么样的 bug? 你可以用比检查 0、1 更好的方式判断输入是否是数字吗? int() 这个函数可以给你一些头绪。
习题36:设计和调试
if语句的规则:
1.每一个"if语句"必须包含一个else。
2.如果这个else永远都不应该被执行到,因为它本身没有任何意义,那你必须在else语句后面使用一个叫做die的函数,让它打印出错误信息并且死给你看,这和上一节的习题类似,这样你可以找到很多的错误。
3."if语句"的嵌套不要超过2层,最好尽量保持只有1层。这意味着如果你在if里边又有了一个if,那你就需要把第二个if移到另一个函数里面。
4.将"if语句"当做段落来对待,其中的每一个if,elif,else组合就跟一个段落的句子组合一样。在这种组合的最前面和最后面留一个空行以作区分。
5.你的布尔测试应该很简单,如果它们很复杂的话,你需要将它们的运算事先放到一个变量里,并且为变量取一个好名字。
循环的规则:
1.只有在循环永不停止时使用"while循环",这意味着你可能永远都用不到。这条只有Python中成立,其他的语言另当别论。
2.其他类型的循环都使用"for循环",尤其是在循环的对象数量固定或者有限的情况下。
调试的小技巧:
1.不要使用 “debugger”。Debugger所作的相当于对病人的全身扫描。你并不会得到某方面的有用信息,而且你会发现它输出的信息态度,而且大部分没有用,或者只会让你更困惑。
2.最好的调试程序的方法是使用print 在各个你想要检查的关键环节将关键变量打印出来,从而检查哪里是否有错。
3.让程序一部分一部分地运行起来。不要等一个很长的脚本写完后才去运行它。写一点,运行一点,再修改一点。
家庭作业略
习题37:复习各种符号
keywords关键字(带#的是不太熟悉或没怎么使用过的)
and
del #
from
not
while
as
elif
global
or
with
assert #
else
if
pass
yield #
break
except
import
class
exec #
in
raise #
continue
finally #
is
return
def
for
lamba #
try #
数据类型
True
False
None
strings
numbers
floats
lists
字符串转义序列(Escape Sequences)
笨方法学python-3(习题10-12)
\\ 反斜杠符号
\'
\"
\a #响铃
\b #退格
\f #换页
\n
\r #回车
\t #横向制表符
\v #纵向制表符
字符串格式化(String Formats)
%d #十进制整数
%i #十进制整数
%o #八进制整数
%u
%x #十六进制整数
%X
%e #指数(基底写为e)
%E #指数(基地写为E)
%f #浮点数
%F
%g #指数(e)或浮点数(根据显示长度)
%G #指数(E)或浮点数(根据显示长度)
%c #单个字符
%r #字符串(采用repr()的显示)
%s #字符串(采用str()的显示)
%% #字符串%
操作符号
+
-
*
** #乘方
/ #浮点数除法
// #整数除法
%
>
<
>=
<=
==
!=
<>
( )
[ ]
{ }
@
,
:
.
=
;
+=
-=
*=
/=
//=
%=
**=
习题38:阅读代码
通读代码并做好标记,标记的内容包括以下几个方面:
1. 函数以及函数的功能。
2. 每个变量的初始赋值。
3. 每个在程序的各个部分中多次出现的变量。它们以后可能会给你带来麻烦。
4. 任何不包含 else 的 if 语句。它们是正确的吗?
5. 任何可能没有结束点的 while 循环。
6. 最后一条,代码中任何你看不懂的部分都记下来。
加分练习
1. 研究一下什么是“流程图(flow chart)”,并学着画一下。
2. 如果你在读代码的时候找出了错误,试着把它们改对,并把修改内容发给作者。
3. 不使用纸质打印时,你可以使用注解符号 # 在程序中加入笔记。有时这些笔记会对后来的读代码的人有很大的帮助。