1、append,extend 用法
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)
#append is a function that lists understand
elements.append(i)#向列表的尾部添加一个新的元素。只接受一个参数
#now we can print them out too
for i in elements:
print("Element was: %d" % i)
#列表是以类的形式实现的。“创建”列表实际上是将一个类实例化。因此,列表有多种方法可以操作。
#1.列表可包含任何数据类型的元素,单个列表中的元素无须全为同一类型。
#2.append() 方法向列表的尾部添加一个新的元素。只接受一个参数。
#3.extend() 方法只接受一个列表作为参数,并将该参数的每个元素都添加到原有的列表中。
# append()用法示例:
# mylist = [1,2,0,'abc']
# mylist.append(4)
# >>> mylist
# [1, 2, 0, 'abc', 4]
# extend()用法示例:
# mylist
# [1, 2, 0, 'abc', 4, 'haha']
# mylist.extend(['lulu'])
# mylist
# [1, 2, 0, 'abc', 4, 'haha', 'lulu']
# mylist.extend(['123123','lalalala'])
# mylist
# [1, 2, 0, 'abc', 4, 'haha', 'lulu', '123123', 'lalalala']
2、地图游戏,对函数的综合运用
from sys import exit
def gold_room():
print("This room is full of gold. How much do you take?")
next = input(">")
if "0" in next or "1" in next:
how_much = int(next)
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 money.")
print("The fat bear is in front of another door.")
print("How are you going to move the bear?")
bear_moved = False
while True:
next = input(">")
if next == "take money":
dead("The bear looks at you then slaps your face off.")
elif next == "taunt bear" and not bear_moved:
print("The bear has moved from the door.You can go through it now.")
bear_moved = True
elif next == "taunt bear" and bear_moved:
dead("The bear gets pissed off and chews your leg off.")
elif next == "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?")
next = input(">")
if "flee" in next:
start()
elif "head" in next:
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?")
next = input(">")
if next == "left":
bear_room()
elif next == "right":
cthulhu_room()
else:
dead("You stumble around the room until you starve.")
start()
#if语句永远要包含一个else
#如果else永远不应该执行到,那么在它后面加一个die函数,让它打印出错误信息并且“死”给你看
#if语句嵌套不要超过2层
#如果布尔测试很复杂,那就先把它们放到一个变量里,再引用变量
#只有永不停止循环才用while,大多数时候用for
#调试程序的办法是用print检查关键环节,而不是写完了再检查,应该做完一环节,检查一环节
#写代码之前,应该设计出流程地图
#先做小版本,再扩充它