这是我的代码:
N_str = input("Input a value: ")
while True:
i=''
if N_str.isdigit and N_str.find('.')==-1:
i=N_str
if int(i)>0:
print(i)
break
else:
N_str = input("Input a value: ")
else:
N_str = input("Input a value: ")
如果输入‘x’就会显示ValueError:invalid literal for int() with base 10: 'x'
但是‘x’在第一个N_str.isdigit不应该就判定为False吗?然后跑else语句吗?
刚刚接触python,辛苦解答一下
代码改一点就行了,这样。
N_str = input("Input a value: ")
while True:
i=''
if N_str.isdigit() and N_str.find('.')==-1:
i=N_str
if int(i)>0:
print(i)
break
else:
N_str = input("Input a value: ")
else:
N_str = input("Input a value: ")
你那样写 N_str.isdigit 返回的是一个对象,而对象是 非0 ,它的返回结果为 True哈!
那也应该是str.isdigit()用法呀。 你单独试试"x".isdigit返回的是True还是False