TypeError: '<' not supported between instances of 'str' and 'int'
2017-04-22 01:02
791 查看
1、错误描述
>>> num=input('请输入一个整数:');
请输入一个整数:78
>>> if num < 10:
num=10;
print("你输入的整数小于10");
elif num < 20:
SyntaxError: invalid syntax
>>> if num<10:
num=10;
print("小于10");
elif num < 20:
num=20;
print("小于20");
else:
print(num);
Traceback (most recent call last):
File "<pyshell#13>", line 1, in <module>
if num<10:
TypeError: '<' not supported between instances of 'str' and 'int'
>>>
2、错误原因 利用input()提示输入的内容是一个字符串,字符串跟整型数值进行比较,类型不匹配导致报错
3、解决办法
在提示输入时,利用int()将字符串转换成整型
num=int(input('请输入一个整数:'));
>>> num=input('请输入一个整数:');
请输入一个整数:78
>>> if num < 10:
num=10;
print("你输入的整数小于10");
elif num < 20:
SyntaxError: invalid syntax
>>> if num<10:
num=10;
print("小于10");
elif num < 20:
num=20;
print("小于20");
else:
print(num);
Traceback (most recent call last):
File "<pyshell#13>", line 1, in <module>
if num<10:
TypeError: '<' not supported between instances of 'str' and 'int'
>>>
2、错误原因 利用input()提示输入的内容是一个字符串,字符串跟整型数值进行比较,类型不匹配导致报错
3、解决办法
在提示输入时,利用int()将字符串转换成整型
num=int(input('请输入一个整数:'));
相关文章推荐
- Python---TypeError: '<' not supported between instances of 'str' and 'int'
- Python3.6:“猜数字游戏”TypeError: '<' not supported between instances of 'str' and 'int'
- TypeError: '<' not supported between instances of 'treeNode' and 'treeNode'
- '>=' not supported between instances of 'str' and 'int'
- 提示错误“'>=' not supported between instances of 'range' and 'int'”
- Python之Headers value 1 must be of type str or bytes, not <class 'int'>错误的解决
- Python字符串与数字拼接报错解决TypeError: cannot concatenate 'str' and 'int' objects
- 关于 "Uncaught TypeError: Property 'submit' of object #<HTMLFormElement> is not a function" 的解决方案
- Python初学者错误:TypeError: unsupported operand type(s) for +: 'int' and 'str'
- Python字符串与数字拼接 TypeError: cannot concatenate 'str' and 'int' objects
- Python错误:TypeError: unsupported operand type(s) for +: 'int' and 'str'
- TypeError: unsupported operand type(s) for +: 'int' and ‘str’问题原因记录
- TypeError: Input 'split_dim' of 'Split' Op has type float32 that does not match expected type of int
- TypeError: unsupported operand type(s) for +: 'int' and 'str'
- Python:TypeError: unorderable types: int() < str()
- SQL Server error "Xml data type is not supported in distributed queries" and workaround for it
- return type of 'main' is not `int'
- python socket send 错误:TypeError: 'str' does not support the buffer interface
- Uncaught TypeErroe: Uncaught TypeError: Cannot call method 'push' of undefined 和 Uncaught TypeError: undefined is not a function
- argument of type 'void (main form::)()' does not match 'void (*)(int)'