Python - 如何创建多个用户输入错误处理?

时间:2018-05-02 23:13:57

标签: python error-handling user-input

我是python的新手,我正在尝试为多个用户输入创建错误处理。我将代码如下所示。 `

#load and filter dataset
import pandas as pd

CITY_DATA = {'Chicago' : 'chicago.csv',
            'New York City':'new_york_city.csv',
            'Washington':'washington.csv'}

#filtering dataset
def get_filters ():

    city_options = ['Chicago','New York City','Washington'.title()]
    month_options = ['January','February','March','April','May','June','July','August','September','November','December','all'.title()]
    day_options = ['Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday','all']

    while True:
        city = input('\nInsert name of the city to analyze! (Chicago, New York City, Washington)\n'.title())
        if city in city_options :
            break
        else:
            print('Your choice is not available. Please try again')

    while True:
        month = input('\nInsert month to filter by or "all" to apply no month filter! (January, February, etc.)\n'.title())
        if month in month_options :
            break
        else:
            print('Your choice is not available. Please try again')

    while True:
        day = input('\nInsert day of the week to filter by or "all" to apply no day filter! (Monday, Tuesday, etc.)\n'.title())
        if day in day_options :
            break
        else:
            print('Your choice is not available. Please try again')

    return city, month, day

但我相信有一个更简单的方法或者可能创建一个函数?任何帮助将不胜感激!

2 个答案:

答案 0 :(得分:0)

您可以使用try / except语句并为所有三个输入循环一次,而不是一次又一次地循环。这就是我想出的。如果它没有帮助,很高兴删除它。

def get_filters():
    city_options, month_options, day_options = ['Chicago','New York City','Washington'.title()], ['January','February','March','April','May','June','July','August','September','November','December','all'.title()], ['Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday','all'.title()]
    while True:
        try:
            city = city_options.index(input('\nInsert name of the city to analyze! (Chicago, New York City, Washington)\n'.title()))
            month = month_options.index(input('\nInsert month to filter by or "all" to apply no month filter! (January, February, etc.)\n'.title()))
            day = day_options.index(input('\nInsert day of the week to filter by or "all" to apply no day filter! (Monday, Tuesday, etc.)\n'.title()))
            return city_options[city], month_options[month], day_options[day]
        except ValueError:
            print ("Your previous choice is not available. Please try again")
print (get_filters())

答案 1 :(得分:0)

您的解决方案的一点重构版本。不确定这是否有很大的帮助。

def get_valid_input(input_msg, options):
    while True:
        entered_value = input(input_msg)
        if entered_value in options:
            break
        else:
            print('Your choice is not available. Please try again')
            continue

def get_filters():
    city_options = ['Chicago', 'New York City', 'Washington'.title()]
    month_options = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'November',
                     'December', 'all'.title()]
    day_options = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday', 'all']

    city_input_msg = 'Insert name of the city to analyze! (Chicago, New York City, Washington): '
    get_valid_input(city_input_msg, city_options)

    month_input_msg = 'Insert month to filter by or "all" to apply no month filter! (January, February, etc.): '
    get_valid_input(month_input_msg, month_options)

    day_input_msg = 'Insert day of the week to filter by or "all" to apply no day filter! (Monday, Tuesday, etc.): '
    get_valid_input(day_input_msg, day_options)

    return city, month, day