1.建一个比较函数,传入的是值
2.不通过的时候,多看题目,很多细节不注意就会错
3.debug两个小时,在想如果就老老实实写if语句,是不是就不用这么久了。。
def num_range(list_num1):
a=False
for i in list_num1:
if i>0 and i<9:
a=True
else:
break
return a
while True:
try:
def num_range(num,j=10):
if num<j:
a=True
else:
a=False
return a
#1 初始化表格是否成功,若成功则返回0, 否则返回-1
row,col=map(int,input().split())
flag1=num_range(row)
flag2=num_range(col)
if flag1 and flag2:
print(0)
else:
print(-1)
#2 要交换的两个单元格的行列值--输出交换单元格是否成功
#题目输入有点问题,题目里输入是分两行,但是运行的时候输入是一行写完
#应该注意在表格范围内
row_c1,col_c1,row_c2,col_c2=map(int,input().split())
flag11=num_range(row_c1,row)
flag12=num_range(col_c1,col)
flag21=num_range(row_c1,row)
flag22=num_range(col_c2,col)
if flag11 and flag12 and flag21 and flag22:
print(0)
else:
print(-1)
#3 输入要插入的行的数值--输出插入行是否成功
#插入行号只允许0~m,超出范围应该返回错误
#比较时应该用更新后的行比
row_a1=int(input())
row2=row+1
flag1=num_range(row_a1,row)
flag2=num_range(row2)
if flag1 and flag2:
print(0)
else:
print(-1)
#4 输入要插入的列的数值--输出插入列是否成功
#插入列号只允许0~n
#比较要跟更新后的列比
col_a1=int(input())
col2=col+1
flag1=num_range(col_a1,col)
flag2=num_range(col2)
if flag1 and flag2:
print(0)
else:
print(-1)
#5 输入要获取运动轨迹的单元格的值-- 输出要查询的运动轨迹的单元查询是否成功
row_check,col_check=map(int,input().split())
flag1=num_range(row_check,row)
flag2=num_range(col_check,col)
if flag1 and flag2:
print(0)
else:
print(-1)
except:
break备注一个混乱的失败思路,建比较函数的时候,想传入一个列表,因此在列表转换里兜了晕了。。。在本地IDE上运行没问题,在牛客环境里不行,最后放弃这个思路了
def num_range(list_num1):
a=False
for i in list_num1:
if i>0 and i<9:
a=True
else:
break
return a
while True:
try:
#功能1
num=map(int,input().split())#可以用list_num接收一行多个值,取值时用for i in list_num
#此时list_num是个迭代器,只可以迭代一次详见补充《Python可迭代对象,迭代器,生成器的区别》
list_num=[]
list_num.append([x for x in list_num])#[[]]
#num只能迭代一次,后面要用到2次,所以要换成list
flag=num_range(list_num[0])
if flag:
print(0)
else:
print(-1)
#功能2
list_num_c1=map(int,input().split())
flag1=num_range(list_num_c1)
list_num_c2=map(int,input().split())
flag2=num_range(list_num_c2)
if flag1 and flag2:
print(0)
else:
print(-1)
#功能3
add_row_list=map(int,input().split())#[]
add_col_list=map(int,input().split())
flag1=num_range(add_row_list)
list_num[0][0]+=1
flag2=num_range(list_num[0])
if flag1 and flag2:
print(0)
else:
print(-1)
#功能4
except:
break补充:Python可迭代对象,迭代器,生成器的区别
参考指路:
(1)https://blog.csdn.net/weixin_33910434/article/details/91477624?utm_medium=distribute.pc_relevant_t0.none-task-blog-BlogCommendFromMachineLearnPai2-1.channel_param&depth_1-utm_source=distribute.pc_relevant_t0.none-task-blog-BlogCommendFromMachineLearnPai2-1.channel_param
(2)https://blog.csdn.net/liangjisheng/article/details/79776008
(3)https://www.zhihu.com/question/20829330
1)可迭代对象包含迭代器。
2)如果一个对象拥有iter方法,其是可迭代对象;如果一个对象拥有next方法,其是迭代器。
3)定义可迭代对象,必须实现iter方法;定义迭代器,必须实现iter和next方法。
你也许会问,结论3与结论2是不是有一点矛盾?既然一个对象拥有了next方法就是迭代器,那为什么迭代器必须同时实现两方法呢?
因为结论1,迭代器也是可迭代对象,因此迭代器必须也实现iter方法。
map()返回值使用一次后变为空——返回的是迭代器
参考指路:
https://blog.csdn.net/qq_39329572/article/details/99684915
可见第二次调用变成了空list。
因为迭代器Iterator会调用方法next()不断指向下一个元素,直到空,报StopIteration错误。
循环取得对象中的值 ,实际上是会调用内部函数next,将数据指向下一个元素。
当多次调用,直到next指向空,此时可迭代对象(链表) 就算到终点了,不能再用了。
类似于 list(A) 或者 for num in A 这样的语句,就是调用了迭代器,执行了next,消耗了迭代对象。所以,再次使用A_object后,会发现它已经空了。
此时,l1已经指向最末尾,空元素了。再次调用next试试
num = l1.next()
Traceback( most recent call last):
Filr "<stdin>" ,line 1 , in <module>
StopIteration
可见,该对象已经到了终点了,不能用了。</module></stdin>

京公网安备 11010502036488号