笨办法学python习题31-35

#习题31:做出决定
print("You enter a dark room with two doors.Do you go through door #1 or door #2?")
door = input(">")

if door =="1":
    print("There's a giant bear here eating a cheese cake.What do you do?")
    print("1. Take the cake.")
    print("2. Scream at the bear.")
    bear = input(">")
    if bear == "1":
        print("The bear eats your face off. Good job!")
    elif bear == "2":
        print("The bear eats your legs off. Good job!")
    else:
        print("Well, doing %s is probably better. Bear runs away." % bear)
elif door == "2":
    print("You stare into the endless abyss at Cthulhu's retina.")
    print("1. Buleberries")
    print("2. Yellow jacket clothespins.")
    print("3. Understanding revolvers yelling melodies.")

    insanity = input(">")
    if insanity == "1" or insanity == "2":
        print("Your body survives powered by a mind of jello. Good job!")
    else:
        print("The insanity rots your eyes into a pool of muck.Good job!")
else:
    print("You stumble around and fall on a knife and die.Good job!")

笨办法学python习题31-35_第1张图片

笨办法学python习题31-35_第2张图片

笨办法学python习题31-35_第3张图片

笨办法学python习题31-35_第4张图片

笨办法学python习题31-35_第5张图片

笨办法学python习题31-35_第6张图片

加分习题

 为游戏添加新的部分,改变玩家做决定的位置。尽自己能力扩展这个游戏,不过别把游泳弄得太怪异了。

#习题32:循环和列表

the_count = [1,2,3,4,5]
fruits = ['apples','oranges','pears','apricots']
change = [1,'pennies',2,'dimes',3,'quarters']
#this first kind of for-loop goes through a list
for number in the_count:
    print("This is count %d" % number)

#same as above
for fruit in fruits:
    print("A fruit of type: %s" % fruit)
#also we can go through mixed lists too
# notice we have to use %r since we don't know what's in it
for i in change:
    print("I got %r" % i)
#we can also build lists,first start with an empty one
elements = []

#then use the range function to do 0 to 5 counts
for i in range(0,6):
    print("Adding %d to the list." % i)
    elements.append(i)

#now we can print them out too
for i in elements:
    print("Element was: %d" % i)

笨办法学python习题31-35_第7张图片

笨办法学python习题31-35_第8张图片

加分习题

1. 注意一下 range 的用法。查一下 range 函数并理解它。

 range() 函数返回的是一个可迭代对象(类型是对象),而不是列表类型, 所以打印的时候不会打印列表。

range(start, stop[, step])

start默认为0;stop必填;step默认为1,必须为非零整数

range返回一个左闭右开[left, right)的序列数

 

2. 在第 22 行,你可以可以直接将 elements 赋值为 range(0,6),而无需使用 for 循环?

elements =  range(0, 6)
for i in elements:
    print("Element was: %d" % i)

笨办法学python习题31-35_第9张图片

3. 在 Python 文档中找到关于列表的内容,仔细阅读以下,除了 append 以外列表还支持哪些操作?

http://www.runoob.com/python3/python3-list.html

#习题33:While循环
i = 0
numbers = []
while i < 6:
    print("At the top i is %d" % i)
    numbers.append(i)
    i = i + 1
    print("Numbers now:",numbers)
    print("At the bottom i is %d" % i)
print("The numbers: ")
for num in numbers:
    print(num)

笨办法学python习题31-35_第10张图片

笨办法学python习题31-35_第11张图片

加分习题

1. 将这个 while 循环改成一个函数,将测试条件(i < 6)中的 6 换成一个变量。

2. 使用这个函数重写你的脚本,并用不同的数字进行测试。

3. 为函数添加另外一个参数,这个参数用来定义第 8 行的加值 + 1 ,这样你就可以让它任意加值了。

4. 再使用该函数重写一遍这个脚本。看看效果如何。

i = 0
numbers = []
n = int(input(">"))
while i < n:
    print("At the top i is %d" % i)
    numbers.append(i)
    i = i + 1
    print("Numbers now:",numbers)
    print("At the bottom i is %d" % i)
print("The numbers: ")
for num in numbers:
    print(num)

笨办法学python习题31-35_第12张图片

 

5. 接下来使用 for-loop 和 range 把这个脚本再写一遍。你还需要中间的加值操作吗?如果你不去掉它,会有什么样的结果?

numbers = []
for i in range(0, 6):
    print("At the top i is %d" % i)
    numbers.append(i)
    print("Number now:", numbers)  # 注意逗号
    print("At the bottom i is %d" % i)
print("The numbers:")
for num in numbers:
    print(num)

#习题 34:访问列表的元素
animals = ['bear','python','peacock','kangaroo','whale','platypus']
print("The animal at 1 is",animals[1])
#2.The 3rd animal.
print("The 3rd animal  is",animals[2])#以下自行添加
#3.The 1st animal.
print("The 1st animal  is %s" % animals[0])
#4.The animal at 3.
print("The animal at 3 is %s " % animals[3])
#5.The 5th animal.
print("The 5th animal is %s" % animals[4])
#6.The animal at 2.
print("The animal at 2 is %s" % animals[2])
#7.The 6th animal.
print("The 6th animal is %s" % animals[5])
#8.The animal at 4.
print("The animal at 4 %s" % animals[3])

笨办法学python习题31-35_第13张图片

--------------------------------------------------
animals = ['bear','python','peacock','kangaroo','whale','platypus']

for i in range(0,6):
    if i == 0 :
        print("The animal at %d is the %dst animal and is a %s" %(i,i+1,animals[i]))
    elif i == 1:
        print("The animal at %d is the %dnd animal and is a %s" %(i,i+1,animals[i]))
    elif i == 2:
        print("The animal at %d is the %drd animal and is a %s" %(i,i+1,animals[i]))
    elif i == 3:
        print("The animal at %d is the %dth animal and is a %s" %(i,i+1,animals[i]))
    elif i == 4:
        print("The animal at %d is the %dth animal and is a %s" %(i,i+1,animals[i]))
    else:
        print("The animal at %d is the %dth animal and is a %s" %(i,i+1,animals[i]))
#习题 35:分支和函数
from  sys import exit

def gold_room():
    print("This 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 greedy bastard!")
def bear_room():
    print("There is a bear here.")
    print("The bear has a bunch of honey.")
    print("The fat bear is in front of another door.")
    print("How are you going to move 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 "feel" 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()

笨办法学python习题31-35_第14张图片

1. 把这个游戏的地图画出来,把自己的路线也画出来。

2. 改正你所有的错误,包括拼写错误。

3. 为你不懂的函数写注解。记得文档注解该怎么写吗?

4. 为游戏添加更多元素。通过怎样的方式可以简化并且扩展游戏的功能呢?

5. 这个? gold_room 游戏使用了奇怪的方式让你键入一个数字。这种方式会导致什么样的 bug? 你可以用比检查 0、1 更好的方式判断输入是否是数字吗? int() 这个函数可以给你一些头绪。
 

 

你可能感兴趣的:(学习笔记,python)