整数输入验证python

时间:2015-05-06 08:05:35

标签: python loops

似乎无法使此循环工作,它会循环回二进制数字输入。我希望它循环回菜单选择。对于noob问题我很抱歉我是python和编程新手。

import sys
loop = 0
menu_Select = 0
for menu_Select in range(1,100):
    #Display user options to the screen
    print('*** Menu ***')
    print('1. Convert to binary')

    userMenu = input('What would you like to do [1,2,3,4]? ')
    if userMenu != '1' and userMenu != '2' and userMenu != '3' and userMenu != '4':
        print("Please enter either 1, 2, 3, or 4.")

    elif userMenu == '4':
        print('Goodbye.')
        sys.exit(0)

    elif userMenu == '1':
        #Decimal to Binary convertion code
        print('\n')
        while loop < 1:
            while True:
                try:
                    user_Number = (int(input('Please enter number: ')))
                except ValueError:
                    print('wrong')
                else:
                    binary_num = []

                    while (user_Number > 0):
                        if user_Number % 2 != 0:
                            binary_num.append(1)
                        elif user_Number % 2 == 0:
                            binary_num.append(0)
                        user_Number = user_Number // 2
                    binary_num.reverse()
                    binary_display = ''.join(str(k) for k in binary_num)
                    print('Binary number: ',binary_display)
            loop += 1

3 个答案:

答案 0 :(得分:2)

如果可以,使用input()实际上会将用户键入的内容转换为int。那么看看会发生什么:

>>> input("= ")
= 12
12

返回12,而不是'12'。为了给我“12”的输入,我需要手动将其包装在引号中。

>>> input("= ")
= '12'
'12'

相反,使用raw_input()让Python读取用户键入的任何内容。

>>> raw_input("= ")
= 12
'12'

此外,正如其他人提到的那样,你使用了while循环错误。如果你想在获得有效数字之前不断询问用户输入,最好用相关条件包装少量代码。

即。只有在没有有效数字的情况下才能运行循环,并且只包含输入发生的行。

        user_Number = None
        while user_Number is None:
            try:
                user_Number = (int(raw_input('Please enter number: ')))
            except ValueError:
                print('wrong')
        binary_num = []

        while (user_Number > 0):
            if user_Number % 2 != 0:
                binary_num.append(1)
            elif user_Number % 2 == 0:
                binary_num.append(0)
                user_Number = user_Number // 2
        binary_num.reverse()
        binary_display = ''.join(str(k) for k in binary_num)
        print('Binary number: ',binary_display)

答案 1 :(得分:1)

您可以在while True循环之前引入布尔变量done = False,并将此循环更改为while not done。然后在打印二进制数后将done设置为True

elif userMenu == '1':
    #Decimal to Binary convertion code
    print('\n')
    done = False
    while not done:
        try:
            user_Number = (int(input('Please enter number: ')))
        except ValueError:
            print('wrong')
        else:
            binary_num = []
            while (user_Number > 0):
                if user_Number % 2 != 0:
                    binary_num.append(1)
                elif user_Number % 2 == 0:
                    binary_num.append(0)
                user_Number = user_Number // 2
            binary_num.reverse()
            binary_display = ''.join(str(k) for k in binary_num)
            print('Binary number: ',binary_display)
            done = True

答案 2 :(得分:0)

变化:

if userMenu != '1' and userMenu != '2' and userMenu != '3' and userMenu != '4':

要:

if userMenu != 1 and userMenu != 2 and userMenu != 3 and userMenu != 4:

并且还更新你的if语句,看它们是否是int而不是字符串。这将适用于python 2.7,不确定python 3。