Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/15.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sql-server-2005/2.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 3.x 我的用户验证有问题_Python 3.x - Fatal编程技术网

Python 3.x 我的用户验证有问题

Python 3.x 我的用户验证有问题,python-3.x,Python 3.x,我试图为一个菜单编写代码,我必须在get_menu()函数中验证用户输入。我的问题是,当我选择菜单时,无法识别输入,如果我选择选项3,然后尝试选择较低的选项,如选项1或2,菜单功能将返回,然后在第二次接受输入时。同样的事情也发生在选项二上,如果我选择一个更低的选项。但是从1-->2、1-->3或2-->3开始,输入在快照上被接受。 我认为这是一个缩进问题,但我看不出来。感谢您提供的任何见解:) #定义空的占位符变量 菜单选项=“” 数字=() 二进制_输出=“” 十进制结果=() #菜单选项的常

我试图为一个菜单编写代码,我必须在get_menu()函数中验证用户输入。我的问题是,当我选择菜单时,无法识别输入,如果我选择选项3,然后尝试选择较低的选项,如选项1或2,菜单功能将返回,然后在第二次接受输入时。同样的事情也发生在选项二上,如果我选择一个更低的选项。但是从1-->2、1-->3或2-->3开始,输入在快照上被接受。 我认为这是一个缩进问题,但我看不出来。感谢您提供的任何见解:)

#定义空的占位符变量
菜单选项=“”
数字=()
二进制_输出=“”
十进制结果=()
#菜单选项的常量
将_转换为_二进制_选项='1'
将\u转换为\u十进制\u选项='2'
二进制计数选择='3'
退出选项='4'
打印(“\n”)
#定义get_menu_choice函数,该函数返回菜单选项值。
def get_菜单_选项():
打印(“***菜单***”)
打印(“\n”)
打印('1.转换为二进制')
打印('2.转换为十进制')
打印('3.二进制计数')
打印('4.退出')
打印(“\n”)
menu_choice=input('您想做什么?[1,2,3,4,]?'))
当您选择菜单时!='1'或菜单选项!='2'或菜单选项!='3'或菜单选项!='4':
输入(“无效输入请输入1、2、3或4:”)
返回菜单选择
其他:
返回菜单选择
def转换为二进制(十进制数):
打印(“\n”)
十进制数=int(输入('请输入数字:'))
而十进制数!=整数(十进制数),范围为(0,9):
int(输入('请输入有效数字:'))
#而十进制数==
二进制_输出=“”
当十进制数>0时:
#这将把余数合并成一个字符串
二进制\u输出+=str(十进制\u数%2)
#这将在10号底面上执行楼层划分
十进制数=(十进制数//2)
#返回二进制输出的值
返回二进制输出
def转换为十进制数(二进制数):
打印(“\n”)
#要转换的二进制输入,以字符串形式输入。
二进制=输入('请输入二进制数:')
#此处已分配空值,准备在for循环中重新分配
十进制=整数()
#对于循环中的每个迭代,分别计算名为binary的字符串中的每个数字
对于二进制数字:
#将十进制结果x 2+整数的值(1或0)存储为十进制
#示例(((1*2+1)*2+0)*2+0)*2+1)
十进制=十进制*2+int(数字)
#循环结束,函数返回十进制的最终值
返回小数
def Binary_counting():
打印(“\n”)
#通过用户输入将十进制数的值声明为整数
十进制数=int(输入('请输入数字:'))
#设置比较循环底部值的计数器
计数器=1
#用户输入用作循环上限的值
上限范围=整数(十进制数)
#主循环使用计数器和上限值
而计数器0:
二进制+=str(十进制数%2)
十进制数=(十进制数//2)
#在显示前反转字符串
二进制=二进制[:-1]
#打印计数器值和二进制值都在这段代码中
打印('Decimal:',计数器,end='')
打印('=二进制:',二进制)
#计数器在这里迭代
计数器=计数器+1
#主while循环以设置菜单选项
当您选择菜单时!=退出选项:
菜单选项=获取菜单选项()
而菜单\u选项==将\u转换为\u二进制\u选项:
数字=将\u转换为\u二进制(数字)
打印(“\n”)
打印('二进制数:',结束='')
打印(编号[:-1])
打印(“\n”)
菜单选项=获取菜单选项()
而菜单\u选项==将\u转换为\u十进制\u选项:
打印(“\n”)
十进制结果=将十进制转换为十进制(十进制结果)
打印(十进制结果)
打印(“\n”)
菜单选项=获取菜单选项()
当菜单\选项==二进制\计数\选项时:
打印(“\n”)
二进制_计数()
菜单选项=获取菜单选项()
如果菜单\选项==退出\选项:
打印(‘再见’)

所以我为用户输入验证提出了这个方法,它运行良好,可以停止alpha输入和空输入:)


print('\n')decimal_number=input('请输入数字:'),而decimal_number.isalpha()或decimal_number='':print('请确保您的数字仅包含数字0-9。“)decimal_number=input('请输入数字:')我现在正在回答我自己的问题:),但我最终找到了解决方案:)
#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')
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: ')