我试图理解为什么 Python 不会进入循环并以错误代码退出。
同时OR
条件正常。
def user_choice():
choice=''
within_range = False
while choice.isdigit == False and within_range == False:
choice=input('Enter valid selection (1-9): ')
if choice.isdigit() == False:
print('You entered non-digit value, please input digit')
if choice.isdigit() == True:
if int(choice) in range(0,10):
within_range=True
else:
within_range=False
return int(choice)
答案 0 :(得分:0)
您在此行中有错误:
while choice.isdigit == False and within_range == False:
您正在将函数str.isdigit
与布尔值False
进行比较
您应该改为评估函数choice.isdigit()
完整固定版本:
def user_choice():
choice = ''
within_range = False
while choice.isdigit() == False and within_range == False:
choice = input('Enter valid selection (1-9): ')
if choice.isdigit() == False:
print('You entered non-digit value, please input digit')
if choice.isdigit() == True:
if int(choice) in range(0, 10):
within_range = True
else:
within_range = False
return int(choice)