#! /usr/bin/env python
# -*- coding: utf-8 -*-
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())