笔记24 笨办法习题34访问列表元素
这个ex34没有习题,只有自己安排的习题。我想把两列列表先加在一起,然后用for循环从列表中分离出来,成为排好名次的分离项。今天一天,就是在做这种尝试,现在还未成功。继续苦想,今天不成功,明天该没有问题吧。
好像这样做有点困难,尝试做了一个ex34.py,结果出了这么一个怪物。
代码ex34.py有点怪,但也算格式上没有问题,可以执行
在这里插入代码片animals = ["bear", "tiger", "penguin", "zebra", "peacock", "whale", "platypus"]
for i in animals:
print(f"This is {i}")
places = animals [0]
i = 0 +1
i +=2
for i in range(1,7):
print(f"insert {i} is in {animals}")
在这里插入代码片PS C:\Users\lenovo\1pythonw> python ex34.py
This is bear
This is tiger
This is penguin
This is zebra
This is peacock
This is whale
This is platypus
insert 1 is in ['bear', 'tiger', 'penguin', 'zebra', 'peacock', 'whale', 'platypus']
insert 2 is in ['bear', 'tiger', 'penguin', 'zebra', 'peacock', 'whale', 'platypus']
insert 3 is in ['bear', 'tiger', 'penguin', 'zebra', 'peacock', 'whale', 'platypus']
insert 4 is in ['bear', 'tiger', 'penguin', 'zebra', 'peacock', 'whale', 'platypus']
insert 5 is in ['bear', 'tiger', 'penguin', 'zebra', 'peacock', 'whale', 'platypus']
insert 6 is in ['bear', 'tiger', 'penguin', 'zebra', 'peacock', 'whale', 'platypus']
PS C:\Users\lenovo\1pythonw>
执行结果,不如人愿自有原因,也许往后再走一段时间,这原因就解释了吧。
再试的结果,出了一个ex34.1.py,要好一些,但还没有达到教材的要求,在此基础上再进一步做出ex34.2.py
练习ex34.1.py
在这里插入代码片animals = ["bear", "tiger", "penguin", "zebra", "peacock", "whale", "platypus"]
for i in animals:
print(f"This is {i}")
places = animals [0]
i = 0 +1
i +=2
for i in range(1,7):
print(f"insert {i} is in {i}")
执行结果
在这里插入代码片PS C:\Users\lenovo\1pythonw> python ex34.1.p
This is bear
This is tiger
This is penguin
This is zebra
This is peacock
This is whale
This is platypus
insert 1 is in 1
insert 2 is in 2
insert 3 is in 3
insert 4 is in 4
insert 5 is in 5
insert 6 is in 6
PS C:\Users\lenovo\1pythonw>
继续试,则出现ex34.2.py
在这里插入代码片i = 0
numbers = [0, 1, 2, 3, 4, 5]
def sixnum(numbers, i) :
print(f"At the top i is {i}")
numbers.append(i)
i = i + 1
print("Numbers now: ", numbers)
print(f"At the bottom i is {i}")
print("The numbers: ")
for num in numbers:
print(num)
执行结果
在这里插入代码片PS C:\Users\lenovo\1pythonw> python ex34.2.py
This is bear
This is tiger
This is penguin
This is zebra
This is peacock
This is whale
This is platypus
append 1 is in 1
append 2 is in 2
append 3 is in 3
append 4 is in 4
append 5 is in 5
append 6 is in 6
该列表动物的英文名字是:bear
该列表动物的英文名字是:tiger
该列表动物的英文名字是:penguin
该列表动物的英文名字是:zebra
该列表动物的英文名字是:peacock
该列表动物的英文名字是:whale
该列表动物的英文名字是:platypus
英文动物名字分别出现,何不将其中文名也循环出现一番,等于是做了巩固练习中的一部分,于是有ex34.3.py。这个代码做好之后,老是出现语法错误,检查多次还有。但感觉不是大问题,应该可以纠正。果然,最后才发现中文英文交替应用时,很容易在标点符号方面混淆,混淆后的差别太小,很难察觉。但只要坚持,这个错误总是能够发现的。
练习ex.34.3.py
在这里插入代码片animals = ["bear", "tiger", "penguin", "zebra", "peacock", "whale", "platypus"]
chinese_animals = ["熊", "虎", "鸽", "斑马", "孔雀", "鲸鱼", "鸭嘴兽"]
for i in animals:
print(f"This is {i}")
places = animals [0]
i = 0 +1
i +=2
for i in range(1,7):
print(f"append {i} is in {i}")
for i in animals:
print(f"该列表动物的英文名字是:{i}")
for i in chinese_animals:
print(f"该动物列表的中文名字是:{i}")
执行结果
在这里插入代码片PS C:\Users\lenovo\1pythonw> python ex34.3.py
This is bear
This is tiger
This is penguin
This is zebra
This is peacock
This is whale
This is platypus
append 1 is in 1
append 2 is in 2
append 3 is in 3
append 4 is in 4
append 5 is in 5
append 6 is in 6
该列表动物的英文名字是:bear
该列表动物的英文名字是:tiger
该列表动物的英文名字是:penguin
该列表动物的英文名字是:zebra
该列表动物的英文名字是:peacock
该列表动物的英文名字是:whale
该列表动物的英文名字是:platypus
该动物列表的中文名字是:熊
该动物列表的中文名字是:虎
该动物列表的中文名字是:鸽
该动物列表的中文名字是:斑马
该动物列表的中文名字是:孔雀
该动物列表的中文名字是:鲸鱼
该动物列表的中文名字是:鸭嘴兽
PS C:\Users\lenovo\1pythonw> python ex34.py
这个习题34就做到这里吧。