笨方法学python-11(习题34-38)

习题34:访问列表的元素python

序数(ordinal number):从1开始编号,表示事物的顺序app

基数(cardinal number):从0开始,表示你能够任意抓取元素ide

1.  上网搜索一下关于序数(ordinal number)和基数(cardinal number)的知识并阅读一下。函数

2. 以你对于这些不一样的数字类型的了解,解释一下为何 “January 1, 2010” 里是2010 而不是 2009?(提示:你不能随机挑选年份。)测试

年份是序数,从1开始,没有0年idea

3. 再写一些列表,用同样的方式做出索引,确认本身能够在两种数字之间互相翻译。spa

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:分支和函数
debug

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

print

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. 不使用纸质打印时,你可使用注解符号  # 在程序中加入笔记。有时这些笔记会对后来的读代码的人有很大的帮助。