第六课:千词斩重写&练习

本节课学习知识点如下(有标注实例,仔细看,不懂找我,24小时在线解答!)

#本周学习的是两种数据类型的使用
#一种是列表(ListType);另一种是None(NoneType)

-------------------------------------------------------------------------------------------

###列表的创建(1、使用中括号【】;2、括号里每个数据使用英文的逗号 ,隔开;3、可以是数字、文字、None)
#nameList = [“张三”,“李四”,1,None]

-------------------------------------------------------------------------------------------

###列表的查找(1、使用下标索引;2、使用index函数返回索引)
#1、根据下标索引找值(使用【索引】,从0开始)
#print(nameList[0])
#print(nameList[1])
#print(nameList[2])
#print(nameList[3])

#输出
<<<张三
<<<李四
<<<1
<<<None


#2、根据index返回值所在的索引位置,返回的是数字类型
a = nameList.index("张三")
b = nameList.index("李四")
c = nameList.index(1)
d = nameList.index(None)

print(a)
print(b)
print(c)
print(d)

#输出
<<<0
<<<1
<<<2
<<<3

------------------------------------------------------------------------------------------------

###列表的删除
#第一种,将不要的位置数据取值换成None,得出【None,“李四”,1,None】
#nameList[0] = None     


#第二种先用index函数取出索引,再用【】进行赋值成None,得出【None,“李四”,1,None】
#a = nameList.index(“张三”)
#nameList[a] = None     

-------------------------------------------------------------------------------------------------

###列表len(长度)的求取(计算列表中有多少个逗号隔开的数据)
#a = len(nameList)
#print(a)

#输出
<<<4

  

书本上的习题

第一题

#首先分析一下这一题涉及知识点的列表的数据创建是用中括号【】,然后使用len()函数计算列表的长度

#创建水果列表
fruitList = ["梨子", "苹果", "香蕉", "橘子", "西瓜", "芒果", "柚子"]

#使用len()函数计算列表的长度,就是计算出列表中有多少个水果数
fruit_num = len(fruitList)

#输出水果数
print(fruit_num)

#输出
<<<6
    

第二题

fruit_en_List = ["Pear", "Apple", "Banana", "Orange", "Watermelon", "Mango", "Pomelo"]
fruit_cn_List = ["梨子","苹果","香蕉","橘子","西瓜","芒果","柚子"]
index = 0
while True:
    fruit_en = fruit_en_List[index]
    fruit_cn = fruit_cn_List[index]
    user = input("请输入" + fruit_en + "的中文翻译是:")
    if user == fruit_cn:
        print("回答正确")
         
    else:
        print("回答错误")
    index = index + 1
    if index = =7:
        break

  

 第三题

 第四题

 

课堂上的作业

第一题

numList = [1, 2, 3, 5, 79, 4, 7, 24, 71, 345, 132, 6756, 31,7865, 95, 13, 72, 55, 476, 77]

 第二题

numList = [1, 2, 3, 5, 79, 4, 7, 24, 71, 345, 132, 6756, 31,7865, 95, 13, 72, 55, 476, 77]
print(numList[0]+ numList[13]+numList[16]+numList[19])


8015

 

第三题

numList = [1, 2, 3, 5, 79, 4, 7, 24, 71, 345, 132, 6756, 31,7865, 95, 13, 72, 55, 476, 77]

numList[16] = None
print(numList[0] + numList[13] +numList[16] +numList[19])


TypeError: unsupported operand type(s) for +: 'int' and 'NoneType'

1、通过None将索引为16的元素删除后,如果直接将它和其它几个元素相加,会报错,因为不同类型之间的数据不能直接相加;
2、应该跳过这个元素,将其它的3个元素进行相加

 

 第四题

#去数的方法,但是有点麻烦
#本题考察的是列表中取值判断的方法
index= 0
while True:
  if numList[index]== None:
    print(index)
    break
  index+=1
  if index>=len(numList):
    break

  

第五题

nameList =["张一","李二","周三","徐四","王五","赵六","吴七"]
while True:
    name = input("请输入学生姓名:")
    print(name +"同学的学号是:"+ str(nameList.index(name))) 

 第六题

 第七题

 第八题

 第九题

 

 

 

 

 

这是上周的作业,希望大家认真完成,回头上课之前会公布答案,遇到不懂的问题要及时和我联系!.

posted @ 2019-11-20 15:29  樵夫-justin  阅读(112)  评论(0)    收藏  举报