Python 验证以测试多个条件

Python validation to test for multiple criteria

编辑:除了 int、str、input、print 之类的东西 - 我不能使用内置函数

我已经搜索了万维网,包括堆栈溢出,但找不到我需要的帮助。

基本上我想要做的是让用户输入一个二进制数,程序将 return 它以十进制格式显示。

需要根据多个条件验证输入:

  1. 必须是整数
  2. 它只能包含 1 或 0 - 它可以包含 0 和 1 的任意组合,但不能包含除 0 或 1 之外的任何数字
  3. 不能为 0(即必须 > 0)

如果满足这 3 个条件,那么它将 运行 将二进制输入转换为十进制数的代码。

我尝试了很多不同的东西,以至于我忘记了它们,但这就是我目前所拥有的,但显然不起作用。

我无法让验证条件全部循环直到满足所有 3 个,然后再进入转换代码。这是我最近的尝试:

#Option 2 Binary to Decimal function
def Binary_to_Decimal(binary_number):
    while binary_number == '0':
        print("Please enter a number greater than 0.")
        binary_number = input("Input a binary number: ")
    decimal_input = 0
    for digit in str(binary_number):
        decimal_input = decimal_input*2 + int(digit)
    print("The decimal value of the number is", decimal_input)

User_Choice = str(input('What would you like to do [1,2,3,4]? '))

play_again = True

while play_again == True:
    if User_Choice == "2":
        print('In command 2 – convert to decimal')
        binary_number = input("Input a binary number: ")
        pending_val = True
        while pending_val == True:
            try:
                input_val = int(binary_number)
            except ValueError:
                print("Input was not a binary number")
                binary_number = input("Input a binary number: ")
            else:
                pending_val = False

            for item in binary_number:
                if item not in {'0', '1'}:
                    print("Please make sure your number contains digits 0-1 only.")
                    binary_number = input("Input a binary number: ")

            if binary_number == '0':
                print("Please enter a number greater than 0.")
                binary_number = input("Input a binary number: ")
            
            else:
                pending_val = False
    
        else:
            Binary_to_Decimal(binary_number)
        User_Choice = str(input('What would you like to do [1,2,3,4]? '))

你应该让你的输入更简单,在每个条件下处理不同的输入是令人困惑的。尝试在循环中使用 continuebreak,这会对您有很大帮助。

#Option 2 Binary to Decimal function
def Binary_to_Decimal(binary_number):
    while binary_number == '0':
        print("Please enter a number greater than 0.")
        binary_number = input("Input a binary number: ")
    decimal_input = 0
    for digit in str(binary_number):
        decimal_input = decimal_input*2 + int(digit)
    print("The decimal value of the number is", decimal_input)

User_Choice = str(input('What would you like to do [1,2,3,4]? '))

play_again = True

while play_again == True:
    if User_Choice == "2":
        print('In command 2 – convert to decimal')
        while True:
            # Only use one input on a loop
            binary_number = input("Input a binary number: ")
            
            # It must be an integer
            try:
                input_val = int(binary_number)
            except ValueError:
                print("Input was not a binary number")
                continue
            
            # It must only contain 1's or 0's - it can contain any combination of 0s and 1s 
            # but it cannot contain any number except 0s or 1s
            if len(binary_number.replace('0', '').replace('1', '')) > 0:
                print("Please make sure your number contains digits 0-1 only.")
                continue
            
            # It cannot be 0 (i.e. it must be > 0)
            if not int(binary_number) > 0:
                print("Please enter a number greater than 0.")
                continue
            
            # if all condition is passed, do the main method
            Binary_to_Decimal(binary_number)
            break
        
        User_Choice = str(input('What would you like to do [1,2,3,4]? '))