|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
1、补写代码,为什么要加入 IsFind 变量?
- name = input('请输入待查找的用户名:')
- score = [['迷途', 85], ['黑夜', 80], ['小布丁', 65], ['福禄娃娃', 95], ['怡静', 90]]
- IsFind = False [color=Red][b]#为什么要引入这个变量?而不是直接在 for 循环中,if 后加 else? [/b][/color]
- for each in score:
- if name in each:
- print(name + '的得分是:', each[1])
- IsFind = True
- break
-
- if IsFind == False:
- print('查找的数据不存在!')
复制代码
我的这种写法,自己运行没有发现问题,不知道是不是有我没发现的bug,请大佬指导:
- name = input('请输入待查找的用户名:')
- score = [['迷途',85],['黑夜',80],['小布丁',65],['福禄娃娃',95],['怡静',90]]
- for each in score:
- if name in each[0]:
- print(name + '的得分是:',each[1])
- break
- else:
- print('查找的数据不存在!')
- Break
复制代码
2、改写sum的问题,为什么 each 不能用 isdigit 判断呢?这里老报错:
- tuple1 = [1,2,3,'a',5,6]
- n = 0
- sums = 0
- for each in tuple1:
- if not each.isdigit(): #错误提示:'int' object has no attribute 'isdigit'
- continue
- else:
- sums = sums + tuple1[n]
- n += 1
复制代码
只有字符串有 isdigit() 。
- >>> '1'.isdigit()
- True
- >>> 1.isdigit()
- SyntaxError: invalid syntax
- >>> [1, 2, 3].isdigit()
- Traceback (most recent call last):
- File "<pyshell#2>", line 1, in <module>
- [1, 2, 3].isdigit()
- AttributeError: 'list' object has no attribute 'isdigit'
复制代码
|
|