功能内的输入验证

时间:2019-03-23 07:51:28

标签: python input

我正在写一个调查表,要求您的用户输入有效的输入。 按照下面的方式,我试图让用户输入一个数字并返回相应的数字的大陆。小于1或大于7的任何字母,字符串或数字均无效,并会提示用户再次输入。

def continent_question() :
    continent = {'0': None, 1: 'asia', 2: 'africa',
                3: 'north america', 4: 'south america',
                5: 'europe', 6: 'oceania', 7: 'antarctica'}
    print('Which continent would you like to travel to?')
    print('  1)', continent[1].title())
    print('  2)', continent[2].title())
    print('  3)', continent[3].title())
    print('  4)', continent[4].title())
    print('  5)', continent[5].title())
    print('  6)', continent[6].title())
    print('  7)', continent[7].title())
    continent_input = int(input('> '))
    while True:
        try:
            continent_input = int(input('> '))
        except ValueError:
            print('I\'m, sorry,')
            continue
        if (continent_input < 1) or (continent_input > 7):
            print('Which continent would you like to travel to?')
            break
    return continent[continent_input]

1 个答案:

答案 0 :(得分:1)

您的问题是您的while循环不会因输入正确而中断。将其分为两个功能可以更轻松:

def get_cq(msg):
    while True:
        try:
            d = int(input(msg))
            if 1 <= d <= 7:
                return d
            else:
                print('Please enter a positive integer between 1 and 7.')
        except ValueError:
            print('Please enter a positive integer between 1 and 7.')



def continent_question():
    continent = {'0': None, 1: 'asia', 2: 'africa',
                3: 'north america', 4: 'south america',
                5: 'europe', 6: 'oceania', 7: 'antarctica'}
    print('Which continent would you like to travel to?')
    print('  1)', continent[1].title())
    print('  2)', continent[2].title())
    print('  3)', continent[3].title())
    print('  4)', continent[4].title())
    print('  5)', continent[5].title())
    print('  6)', continent[6].title())
    print('  7)', continent[7].title())
    continent_input = get_cq('Which continent would you like to travel to? ')

    return continent[continent_input]

x = continent_question()
print('Your answer is', x)

编辑:为回答您的问题,我总是使用单独的函数进行输入检查,因为我发现没有一个函数就很麻烦。请记住,您可以在另一个函数中定义一个函数,如下所示:

def continent_question():

    def get_cq(msg):
        while True:
            try:
                d = int(input(msg))
                if 1 <= d <= 7:
                    return d
                else:
                    print('Please enter a positive integer between 1 and 7.')
            except ValueError:
                print('Please enter a positive integer between 1 and 7.')

    continent = {'0': None, 1: 'asia', 2: 'africa',
                3: 'north america', 4: 'south america',
                5: 'europe', 6: 'oceania', 7: 'antarctica'}
    print('Which continent would you like to travel to?')
    print('  1)', continent[1].title())
    print('  2)', continent[2].title())
    print('  3)', continent[3].title())
    print('  4)', continent[4].title())
    print('  5)', continent[5].title())
    print('  6)', continent[6].title())
    print('  7)', continent[7].title())
    continent_input = get_cq('Which continent would you like to travel to? ')

    return continent[continent_input]

x = continent_question()
print('Your answer is', x)

以免使您的其余代码混乱。

第二个问题:

#https://stackoverflow.com/questions/55311752/input-validation-within-function/55311826#55311826

def continent_question():

    def get_cq(msg):
        while True:
            try:
                d = input(msg)
                if 1 <= int(d) <= 7:
                    return int(d)
                else:
                    print(f'\'{d}\' is an invalid input. Please enter a positive integer between 1 and 7. Your Options:')
                    print('  1)', continent[1].title())
                    print('  2)', continent[2].title())
                    print('  3)', continent[3].title())
                    print('  4)', continent[4].title())
                    print('  5)', continent[5].title())
                    print('  6)', continent[6].title())
                    print('  7)', continent[7].title())
            except ValueError:
                print(f'\'{d}\' is an invalid input. Please enter a positive integer between 1 and 7. Your Options:')
                print('  1)', continent[1].title())
                print('  2)', continent[2].title())
                print('  3)', continent[3].title())
                print('  4)', continent[4].title())
                print('  5)', continent[5].title())
                print('  6)', continent[6].title())
                print('  7)', continent[7].title())

    continent = {'0': None, 1: 'asia', 2: 'africa',
                3: 'north america', 4: 'south america',
                5: 'europe', 6: 'oceania', 7: 'antarctica'}
    print('Which continent would you like to travel to?')
    print('  1)', continent[1].title())
    print('  2)', continent[2].title())
    print('  3)', continent[3].title())
    print('  4)', continent[4].title())
    print('  5)', continent[5].title())
    print('  6)', continent[6].title())
    print('  7)', continent[7].title())
    continent_input = get_cq('Which continent would you like to travel to? ')

    return continent[continent_input]

x = continent_question()
print('Your answer is', x)