我的用户验证有问题

时间:2018-09-22 00:07:43

标签: python-3.x

我正在尝试将菜单代码组合在一起,并且必须验证get_menu()函数中的用户输入。我的问题是做出菜单选择时无法识别输入,如果我选择选项3,然后尝试选择一个较低的选项(例如选项1或2),则菜单功能会恢复,然后在第二次输入公认。如果选择第二个选项,第二个选项也会发生同样的事情。但是从1-> 2或1-> 3或2-> 3出发,输入将在镜头上起作用。  我认为这是一个缩进问题,但我看不到。感谢您的任何见解:)

#define empty place holder variables
menu_choice = ''
number = ()
Binary_Output = ''
decimal_result = ()

#Constants for the menu choices
CONVERT_TO_BINARY_CHOICE = '1'
CONVERT_TO_DECIMAL_CHOICE = '2'
BINARY_COUNTING_CHOICE = '3'
QUIT_CHOICE = '4'

print('\n')
#Defining the get_menu_choice function, which returns the menu choice value.
def get_menu_choice():
    print ('    *** Menu ***')
    print ('\n')
    print ('1. Convert to binary')
    print ('2. Convert to decimal')
    print ('3. Binary counting')
    print ('4. Quit')
    print ('\n')
    menu_choice = input('What would you like to do? [1, 2, 3, 4,]?')
    while menu_choice != '1' or menu_choice != '2' or menu_choice != '3' or menu_choice != '4':
        input("Invalid input please enter either 1, 2, 3, or 4: ")
        return menu_choice
    else:
        return menu_choice

def convert_to_binary(decimal_number):
    print('\n')
    decimal_number = int(input('Please enter number: '))
    while decimal_number != int(decimal_number) in range(0,9):
        int(input('Please enter a valid number: '))
    #while decimal_number == 
    Binary_Output = ''
    while decimal_number > 0:
        #This will concantenate the remainder into a string
        Binary_Output += str(decimal_number % 2)
        #This will perform floor division on the base 10 number
        decimal_number = (decimal_number // 2)
    #the value of Binary_Output is returned
    return Binary_Output


def convert_to_decimal(binary_number):
    print('\n')
    #The binary input to be converted, entered as a string.
    binary = input('Please enter binary number: ')
    #Assigned an empty value here ready to be reassigned in the for loop
    decimal = int()
    #For each iteration in the loop,  indiviually evaluate each digit in the string named binary
    for digit in binary:
        #Stores the result of decimal x 2 + value of the integer either 1 or 0 to decimal 
        #example (((1 * 2+1)*2+0)*2+0)*2+1
        decimal = decimal * 2 + int(digit)
    #loop is finished and the funtion returns the final value for decimal
    return decimal

def Binary_counting():
    print('\n')
    #Declaring the value of decimal_number through user input as an integer
    decimal_number = int(input('Please enter number: '))
    #A counter to set the bottom value of the the comparison loop
    counter = 1
    #User input is used as the value for the loop's upper limit
    upper_range = int(decimal_number)
    #The main loop using the counter and upper limit values
    while counter <= upper_range:
        #empty local values for the function
        binary = ''
        decimal_number = counter
        #Binary conversion of the decimal numbers until it reaches the upper limit
        while decimal_number > 0:
            binary += str(decimal_number % 2)
            decimal_number = (decimal_number // 2)
        #Reverse the string before displaying it
        binary = binary[::-1]
        #Printing of the counter value and binary value are in this code
        print('Decimal:', counter, end=' ',)
        print(' = binary:' , binary)
        #Counter is iterated here
        counter = counter + 1


#Main while loop to set the menu choice
while menu_choice != QUIT_CHOICE:
    menu_choice = get_menu_choice()


    while menu_choice == CONVERT_TO_BINARY_CHOICE:
        number = convert_to_binary(number)
        print('\n')
        print ('Binary Number : ', end=' ')
        print(number[::-1])
        print('\n')
        menu_choice = get_menu_choice()

    while menu_choice == CONVERT_TO_DECIMAL_CHOICE:
        print('\n')
        decimal_result = convert_to_decimal(decimal_result)
        print(decimal_result)
        print('\n')
        menu_choice = get_menu_choice()

    while menu_choice == BINARY_COUNTING_CHOICE:
        print('\n')
        Binary_counting()
        menu_choice = get_menu_choice()

if menu_choice == QUIT_CHOICE:
    print('Goodbye')

1 个答案:

答案 0 :(得分:0)

所以我想出了这个用于用户输入验证,它可以很好地工作并停止alpha输入和空输入:)

print('\n')
decimal_number = input('Please enter number: ')
while decimal_number.isalpha() or decimal_number == '':
    print("Please make sure your number contains digits 0-9 only. ")
    decimal_number = input('Please enter number: ')