“笨办法”学Python笔记 Lesson35—45

本文介绍了笨办法学Python的第35至45课的作业内容,包括if语句的设计与调试规则,强调简单布尔测试和避免过度嵌套。此外,还讲解了Python中的循环规则,推荐使用for循环,并分享了调试程序的最佳实践,如使用print语句。同时,复习了Python的关键字、操作符、字符串转义序列和格式化,以及加分习题的建议,如研究面向对象编程和dict操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、作业内容

笨办法学 Python 习题35-45以及加分题。


二、作业代码:

#习题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()

 

习题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()的显示)

%%    #字符串%

操作符号

+

-

*

**    #乘方

/    #浮点数除法

//    #整数除法

%

>

<

>=

<=

==

!=

<>

( )

[ ]

{ }

@

,

:

.

=

;

+=

-=

*=

/=

//=

%=

**=


 

习题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()的显示)

%%    #字符串%

操作符号

+

-

*

**    #乘方

/    #浮点数除法

//    #整数除法

%

>

<

>=

<=

==

!=

<>

( )

[ ]

{ }

@

,

:

.

=

;

+=

-=

*=

/=

//=

%=

**=


# 习题 39: 列表的操作

ten_things = "Apples Oranges Crows Telephone Light sugar"



print("Wait there's not 10 things in that list, let's fix that.")

# ten_things.split(' ')其实是.split(ten_things, ' ')

stuff = ten_things.split(' ')

more_stuff = ["Day", "Night", "Song", "Frisbee", "Corn", "Banana", "Girl", "Boy"]



while len(stuff) != 10:

    next_one = more_stuff.pop()

    print("Adding: ", next_one)

    stuff.append(next_one)

    print("There's %d items now." % len(stuff))



print("There we go: ", stuff)



print("Let's do some things with stuff.")



print(stuff[1])

print(stuff[-1])

print(stuff.pop())

print(' '.join(stuff))

print('#'.join(stuff[3:5]))

加分习题

1.将每一个被调用的函数以上述的方式翻译成 Python 实际执行的动作。例如: ' '.join(things) 其实是 join(' ', things) 。

2.将这两种方式翻译为自然语言。例如, ' '.join(things) 可以翻译成“用 ‘ ‘ 连接(join) things”,而 join(' ', things) 的意思是“为 ‘ ‘ 和 things 调用 join 函数”。这其实是同一件事情。

3.上网阅读一些关于“面向对象编程(Object Oriented Programming)”的资料。晕了吧?嗯,我以前也是。别担心。你将从这本书学到足够用的关于面向对象编程的基础知识,而以后你还可以慢慢学到更多。

4.查一下 Python中的 “class” 是什么东西。不要阅读关于其他语言的 “class” 的用法,这会让你更糊涂。

5.dir(something) 和 something 的 class 有什么关系?

6.如果你不知道我讲的是些什么东西,别担心。程序员为了显得自己聪明,于是就发明了 Opject Oriented Programming,简称为 OOP,然后他们就开始滥用这个东西了。如果你觉得这东西太难,你可以开始学一下 “函数编程(functional programming)”。

# 加分习题1.2.

ten_things = "Apples Oranges Crows Telephone Light sugar"



print("Wait there's not 10 things in that list, let's fix that.")



# ten_things.split(' ') 其实是split(ten_things, ' ')

# ten_things.split(' ') 翻译为:用ten_things split(分割) ' '

# split(ten_things, ' ') 翻译为:为ten_things和' '调用split(分割)函数

stuff = ten_things.split(' ')

more_stuff = ["Day", "Night", "Song", "Frisbee", "Corn", "Banana", "Girl", "Boy"]



while len(stuff) != 10:

    next_one = more_stuff.pop()

    print("Adding: ", next_one)



    # stuff.append(next_one)其实是append(stuff, next_one)

    # stuff.append(next_one) 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值