Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/351.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 为什么不是';我的输入错误陷阱没有按照我的预期工作吗?_Python_Input_Error Handling - Fatal编程技术网

Python 为什么不是';我的输入错误陷阱没有按照我的预期工作吗?

Python 为什么不是';我的输入错误陷阱没有按照我的预期工作吗?,python,input,error-handling,Python,Input,Error Handling,为什么这个错误陷阱不起作用?我想让它,如果有人键入一个单词或数字超过1,它会错误陷阱,并重复问题,直到您键入正确的相应输入。有什么建议吗 valid = 0 while valid ==0: x = input('Enter 4 digits for X(0 or 1): ') while len(x)!=4: print('Try Again. Remember 4 digits and only use binary 1 or

为什么这个错误陷阱不起作用?我想让它,如果有人键入一个单词或数字超过1,它会错误陷阱,并重复问题,直到您键入正确的相应输入。有什么建议吗

valid = 0
    while valid ==0:
        x = input('Enter 4 digits for X(0 or 1): ')
        while len(x)!=4:
            print('Try Again. Remember 4 digits and only use binary 1 or 0')
            x = input('Enter 4 digits for X: ')
        valid = 1
        for i in x:
            if i !='1' and i !='0':
                print('Try Again. Remember 4 digits and only use binary 1 or 0')
                valid=0



    valid = 0
    while valid ==0:
        y = input('Enter 4 digits for Y(0 or 1): ')
        while len(y)!=4:
            print('Try Again. Remember 4 digits and only use binary 1 or 0')
            y = input('Enter 4 digits for y: ')
        valid = 1
        for i in y: 
            if i !='1' and i !='0':
                print('Try Again. Remember 4 digits and only use binary 1 or 0')
                valid=0

您应该使用
if
语句而不是
while
循环来测试输入的长度。您还可以使用
all(映射('01'。\uuu包含\uuuu,值))
来测试字符串是否仅由“0”或“1”字符组成。您可以将
x
y
作为变量名的元组,并通过相同的代码迭代,以相同的逻辑请求它们的值,然后将该值作为全局变量或dict的值进行赋值

for variable in 'x', 'y':
    while True:
        value = input('Enter 4 digits for %s (0 or 1): ' % variable)
        if len(value) == 4 and all(map('01'.__contains__, value)):
            globals()[variable] = value
            break
        print('Try Again. Remember 4 digits and only use binary 1 or 0')
print(x, y)

我建议将代码放在函数中,以帮助将内容拆分为更小的部分,并使用正则表达式测试是否存在除1或0以外的字符。以下是基于您提供的代码的建议(这里的代码是针对Python3+):


在这个实例中,类包含什么特殊方法??只是为了完全理解它。\ uu包含\uuuu('1'),例如,相当于'01'中的
'1'<代码>\uuuu包含\uuuu
测试项是否是从中调用它的对象的成员。在这种情况下,它测试
的每个字符是否是“0”或“1”中的一个,因为Python中的字符串是序列类型,所以当字符串
作为序列映射到序列“01”的
包含方法时。你可以阅读更多关于
\uuuuuuuuuuuuuuuuuuuuuuuuuuuu包含的内容。当然可以!再次感谢!我在我的课本上读到了这个。很高兴看到它在使用。
import re

def check_input(input_str):
    # Regex for function, look for any single char not 1 or 0
    regex = r"[^10]"

    # Check if input is 4 bits
    if len(input_str) != 4:
        print('Sorry, input must only be 4 bits')
        return False

    # Check if characters are only 1 or 0
    if re.search(regex, input_str):
        print('Sorry, input must be composed of only 1s or 0s')
        return False

    return True

def get_binary_input():
    good_input = False
    input_str = ""
    while not good_input:
        input_str = input('Enter 4 digits for X(0 or 1): ')
        good_input = check_input(input_str)
    return input_str

def main():
    binary_str = get_binary_input()
    print('Got binary string: {}'.format(binary_str))
    return

if __name__ == '__main__':
    main()