无法理解为什么和不起作用

时间:2020-10-17 06:55:26

标签: python while-loop

我试图理解为什么 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)

1 个答案:

答案 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)
相关问题