记得改参数
第七章本章作业
**
**
2、编码题
(1)、list1 = [1,2,3,4]
list1[5] = 9
print(list1)
(2)、list1 = []
for i in range(1,6):
print("请输入第%d个数"%(i))
students = int(input())
list1.append(students)
list1.sort()
if students < 0 :
print("输入有误,请输入正确的信息")
elif students >100 :
print("输入有误,请输入正确的信息")
print(“你输入的数是:”,list1)
(3)、import time
list1 = []
for i in range(1,6):
print("请输入第%d个数"%(i))
students = int(input())
if students < 0:
print("输入有误,请输入正确的信息")
tme = time.strftime("%Y-%m-%d %H:%M:%S")
print(tme)
continue
elif students >100 :
print("输入有误,请输入正确的信息")
tme = time.strftime("%Y-%m-%d %H:%M:%S")
print(tme)
continue
list1.append(students)
list1.sort()
while len(list1) < 5:
print("输入的数字不够")
again = int(input())
list1.append(again)
list1.sort()
print(“你输入的数是:”,list1)
tme = time.strftime("%Y-%m-%d %H:%M:%S")
print(tme)
(4)、ls = [15,8,7,9,8]
def mid_mean(ls):
new_list = []
for i in range(len(ls)):
new_item = (ls[i-1]+ls[i]+ls[i+1])/3
new_list.append(new_item)
return new_list
原因是return只能写在def里面的函数里面,不能缩进出来
本篇博客详细解析了Python编程中的常见错误与注意事项,通过具体的编码练习题,如列表操作、条件判断、时间戳获取及函数定义等,帮助读者深入理解Python编程的基础知识。特别强调了在函数中使用return语句的正确方式。
2473

被折叠的 条评论
为什么被折叠?



