def absolute(x):
if x>
0:
return x
else:
return -x
print(
'Please enter a num:')
num = input()
print(absolute(num))
运行以上代码,报错信息如下:
Traceback (most recent call last): File “5_func.py”, line 12, in print(absolute(num)) File “5_func.py”, line 5, in absolute if x>0: TypeError: unorderable types: str() > int()
报错原因: python3里面input默认接收到的是str类型,返回str类型
解决: 将input返回值进行强制转换,如下
num = int(input())
转载请注明原文地址: https://ju.6miu.com/read-30421.html