因为一个变量命名的关系中断了一个while 循环,有点意思,什么原因?
IDE: Pycharm Community Edition
一、变量命名为 input , 中断了while 循环,不能循环提示输入:
def isNum(x):
if isinstance(x,int):
return True
elif isinstance(x,float):
return True
elif isinstance(x,complex):
return True
else:
return False
while True:
try:
input = eval(input("please input a data:"))
except:
continue
if isNum(input):
print('true')
else:
print('false')
输出:
please input a data:abc
please input a data:1
true
二、变量命名改为 value , 有效while 循环能循环提示输入:
def isNum(x):
if isinstance(x,int):
return True
elif isinstance(x,float):
return True
elif isinstance(x,complex):
return True
else:
return False
while True:
try:
value = eval(input("please input a data:"))
except:
continue
if isNum(value):
print('true')
else:
print('false')
输出:
please input a data:abc
please input a data:1
true
please input a data:2
true
please input a data: