|
|

楼主 |
发表于 2019-11-1 05:39:42
|
显示全部楼层
import random as r
a = ['int','float','bool','str','complex']
def random_converter(x):
value=r.choice(a)
if value == 'int':
if type(x) == complex:
print('connot be converted')
return None
else:
x = int(x)
print(x,type(x))
elif value == 'float':
if type(x) == complex:
print('connot be converted')
return None
else:
x = float(x)
print(x,type(x))
elif value == 'str':
x = str(x)
print(x,type(x))
elif value == 'bool':
x = bool(x)
print(x,type(x))
elif value == 'complex':
x = complex(x)
print(x,type(x))
大佬,我写到这个地步了,但是比如 'aaaaa'转complex,和整数和浮点数就会报错而不是NONE。就这里不知道怎么解决,求教一下。 |
|