34.访问列表元素
小结:这节主要讲了列表基数(从0开始)与序数(从1开始)的区别
练习:
animals = ["bear", "python3.6", "peacock", "kangaroo", "whale", "platypus"]
print("位置为1的动物是第二只动物,是python3.6", animals[1])
print("第三只动物位置为2的动物,是peacock", animals[2])
print("第一只动物位置为0的动物,是bear", animals[0])
print("位置为3的动物是第四只动物,是kangaroo", animals[3])
print("第五只动物位置为4的动物,是whale", animals[4])
print("位置为3的动物是第四只动物,是kangaroo", animals[3])
print("第六只动物位置为5的动物,是platypus", animals[5])
print("位置为4的动物是第五只动物,是whale", animals[4])
35.分支与函数
小结:这节主要讲了if语句,函数的混用,并以一个游戏的形式呈现,使用while True可以写一个无限循环
练习:
from sys import exit #从sys模块中导入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 bunck 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 bear" and not bear_moved:
print("The bear has move from the door.")
print("You can go through it now.")
bear_moved = True #循环条件被修改
elif choice == "taunt bear" 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 you 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()
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:
print("You stumble arount the room until you starve.")
start()
36.调试和设计
小结:这节主要讲了判断语句if及循环语句while,for的循环规则,复杂的程序一部分一部分运行起来,不要写了一大串再去运行
if语句规则 | 1.每条if都需要包含一个else 2.if语句嵌套不要超过两层 3.布尔测试应该相对简单一些,如果很复杂,要先将运算事先放到一个变量中 |
循环语句规则 | 1.在永不停止时使用while语句 2.尽量使用for语句 |
37.复习各种符号(略)
38.列表的操作
小结:数据结构是组织数据的正式方式,其中最常用的数据结构即为列表,就像一摞纸牌,两者对比:
列表 | 纸牌 |
有序的列表 | 纸牌从头到尾有序排列 |
要储存的东西 | 即纸牌本身内容 |
随机访问 | 可以从纸牌中任意抽取一张 |
线性 | 如果要找到某张牌,可以从第一张开始寻找 |
通过索引 | 得知位置后可以找出 |
练习:
ten_things = "Apples Oranges Crows Telephone Light Sugar"
stuff = ten_things.split(" ") # 根据空格进行slipt
# print(">>>",ten_things)
print(f"Wait! there are just {len(stuff)} things in that list. Let's fix that.")
# print(">>>",stuff)
more_stuff = ["Day", "Night", "Song", "Frisbee", "Corn", "Banana", "Girl", "Boy"]
# print(">>>",more_stuff)
# 将more_stuff列表中从前往后数删除的第一个赋值为next_oneone
while len(stuff) != 10:
next_one = more_stuff.pop(0) #.pop()默认是从后往前
print("Adding: ", next_one)
# print(">>>Now more_stuff list exists: ", more_stuff)
stuff.append(next_one)
print(f"There are {len(stuff)} items now,\n they are {stuff}")
print("Let's do some things with stuff.")
print(stuff[1])
print(stuff[-1])
print(stuff.pop())
# print(">>>",stuff)
print(' '.join(stuff)) #在stuff的元素之间插入空格
print('#'.join(stuff[3:5])) #在基数为3和4的元素之间插入 # ,同range一样,上限不在内原则
课后练习需要用for语句来替换一下这里的while,11-16替换结果如下:
for i in more_stuff:
stuff.append(i)
if len(stuff) == 10:
break
print(f"There are {len(stuff)} items now,\n they are {stuff}")
print(stuff)