Traceback (most recent call last): File "C:/Users/Sure/PyProject/day01/Homework/11 成绩档次.py", line 3, in <module> if score >= 90: TypeError: '>='not supported between instances of 'str'and'int'
Traceback (most recent call last): File "C:/Users/Sure/PyProject/day05/01 exercise.py", line 58, in <module> dic["alex": 89] TypeError: unhashable type: 'slice'
错误代码:
1 2 3
dic = {"key": 1} dic["alex": 89] print(dic)
错误原因:
字典增加操作的输入不规范,正确格式为:字典["键"] = "值"。
解决方法:
修改为正确格式
1 2
dic = {'alex': {'a': 1}} print(dic)
an integer is required (got type str)
错误6.1
报错信息:
1 2 3 4
Traceback (most recent call last): File Assignment, line 10, in <module> withopen('t1.txt', 'r', 'utf-8') as f: TypeError: an integer is required (got typestr)
错误代码:
1 2 3
withopen('t1.txt', 'r', 'utf-8') as f: for line in f: print(line)
错误原因:
文件打开操作时,编码方式需要指定参数名为encoding
解决方法:
在编码方式utf-8前指明参数名。
1 2 3
withopen('t1.txt', 'r', encoding='utf-8') as f: for line in f: print(line)
‘builtin_function_or_method’ object is not subscriptable
错误7.1
报错信息:
1 2 3 4
Traceback (most recent call last): File "C:/Users/Sure/PyProject/day07/01 exercise.py", line 69, in <module> l.append[2] TypeError: 'builtin_function_or_method'objectisnot subscriptable
错误代码:
1 2 3 4
l = [] dic = dict.fromkeys('abc', l) l.append[2] print(l, dic)
错误原因:
方法的调用需要使用小括号而不是中括号,中括号是索引,小括号才是调用。
解决方法:
将中括号改为小括号。
1 2 3 4
l = [] dic = dict.fromkeys('abc', l) l.append(2) print(l, dic)
eat() missing 2 required keyword-only arguments: ‘a’ and ‘b’
错误8.1
报错信息:
1 2 3 4
Traceback (most recent call last): File "C:/Users/Sure/PyProject/day10/exercise.py", line 12, in <module> eat("面条","米饭","馒头","包子","煎饼") TypeError: eat() missing 2 required keyword-only arguments: 'a'and'b'
unsupported operand type(s) for -: ‘builtin_function_or_method’ and ‘builtin_function_or_method’
错误11.1
报错信息:
1 2 3 4 5
Traceback (most recent call last): i am in index File "C:/Users/Sure/PyProject/day13/exercise.py", line 162, in <module> print(time.time - start_time) TypeError: unsupported operand type(s) for -: 'builtin_function_or_method'and'builtin_function_or_method'
Traceback (most recent call last): File "C:/Users/Sure/PyProject/week06/day22/exercise.py", line 115, in <module> xiaoming.game(xiaohong) File "C:/Users/Sure/PyProject/week06/day22/exercise.py", line 107, in game print('%s和%s快乐地玩耍' % self.name, girl.name) TypeError: not enough arguments forformat string
错误代码:
1
print('%s和%s快乐地玩耍' % self.name, girl.name)
错误原因:
格式化字符串时,如果有多个位置要格式化,后面的数据要放到括号中
解决方法:
将数据用括号括起来。
1
print('%s和%s快乐地玩耍' % (self.name, girl.name))
__str__ returned non-string (type int)
错误15.1
报错信息:
1 2 3 4
<__main__.A object at 0x00000230E107B6D8> File "C:/Users/Sure/PyProject/week07/day24/exercise.py", line 55, in <module> print(b) TypeError: __str__ returned non-string (typeint)
错误代码:
1 2 3 4 5 6 7 8 9
classA: pass classB: def__str__(self): return12 a = A() b = B() print(a) print(b)
错误原因:
魔法方法__str__的返回值只能是字符串类型
解决方法:
将返回值改成字符串
1 2 3 4 5 6 7 8 9
classA: pass classB: def__str__(self): return'这时B对象' a = A() b = B() print(a) print(b)