#代码方便测试改成了s=None,使用的时候改称s=input(),去掉return a b d这些
def select_num(s=None):
try:
b=float(s)
a=int(b)
if a-b==0:
return a
print('整数')
if a-b!=0:
return b
print('浮点数')
except:
try:
d=complex(s)
return d
print('虚数')
except:
a='输入的不是数字'
return a
print('输入的不是数字')
#测试
import unittest
class test(unittest.TestCase):
def test01(self):
t=select_num(s='1.1')
self.assertEqual(type(t),float)
def test02(self):
t=select_num(s='1')
self.assertEqual(type(t),int)
def test03(self):
t=select_num(s='+1')
self.assertEqual(type(t),int)
def test04(self):
t=select_num(s='-1')
self.assertEqual(type(t),int)
def test05(self):
t=select_num(s='-1.1')
self.assertEqual(type(t),float)
def test06(self):
t=select_num(s='+1.1')
self.assertEqual(type(t),float)
def test07(self):
t=select_num(s='0j')
self.assertEqual(type(t),complex)
def test08(self):
t=select_num(s='1+0j')
self.assertEqual(type(t),complex)
def test09(self):
t=select_num(s='abc')
self.assertEqual(t,'输入的不是数字')
if __name__ == '__main__':
unittest.main()