2

I'm new to Python and could really use some help on this. I want to create a function to filter which files I want to open and which months and day specifically. That way, the users need to input which city(files) they want to analyze on which particular month or day. However, I want the user to be able to input something that is not case sensitive. For example, the user can input 'chicago'/'CHICAGO"/"ChIcAgO" and the it still give you the right output and not the error handling message. Here is the code I use:

def get_filters():
    """
    Asks user to specify a city, month, and day to analyze.

    Returns:
        (str) city - name of the city to analyze
        (str) month - name of the month to filter by, or "none" to apply no month filter
        (str) day - name of the day of week to filter by, or "none" to apply no day filter
    """
    print('Hello! Let\'s explore some US bikeshare data!')

    # get user input for city (Chicago, New York, Washington). HINT: Use a while loop to handle invalid inputs
    valid_cities = ['Chicago', 'New York', 'Washington']
    city = valid_cities.index(input('Would you like to filter the data for Chicago, New York or Washington? \n')
    while not city in valid_cities:
        city = input('Selection error! Please choose a vaild city \n')
    print("you chose %s." % city)

    # get user input for month (all, January, February, ... , June)
    valid_months = ['January', 'February', 'March', 'April', 'May', 'June', 'none']
    month = input('Would like to filter data by month? Type e.g. "May" for month, or "none" for no month filter \n')
    while not month in valid_months:
        month = input('Selection error! Please enter a valid month or "none" for no month filter. \n')
    print("you chose %s." % month)

    # get user input for day of week (all, Monday, Tuesday, ... Sunday)
    days = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday', 'none']
    day = input('Which day? Please type a day, e.g. "Monday", or type "none" for no day filter \n')
    while not day in days:
        day = input('Selection error! Please enter a valid day or "none" for no day filter. \n')
    print("you chose %s." % day)

    print('-'*40)
    return city, month, day


def load_data(city, month, day):
    """
    Loads data for the specified city and filters by month and day if applicable.

    Args:
        (str) city - name of the city to analyze
        (str) month - name of the month to filter by, or "none" to apply no month filter
        (str) day - name of the day of week to filter by, or "none" to apply no day filter
    Returns:
        df - Pandas DataFrame containing city data filtered by month and day
        Start Time,End Time,Trip Duration,Start Station,End Station,User Type,Gender or "No gender data to share" if no data ,Birth Year
        1423854,2017-06-23 15:09:32,2017-06-23 15:14:53,321,Wood St & Hubbard St,Damen Ave & Chicago Ave,Subscriber,Male,1992.0
    """

    df = pd.read_csv(CITY_DATA[city])
    df['Start Time'] = pd.to_datetime(df['Start Time'])
    df['month'] = df['Start Time'].dt.month
    df['day_of_week'] = df['Start Time'].dt.weekday_name

    if month != 'none':
        # use the index of the months list to get the corresponding int
        months = ['January', 'February', 'March', 'April', 'May', 'June']
        month = months.index(month) + 1
        df = df[df['month'] == month]

    if day != 'none':
        days = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']
        df = df[df['day_of_week'] == day.title()]
        df = df[df['day_of_week'] == day]

    return df
vahdet
  • 6,357
  • 9
  • 51
  • 106
colm6287
  • 21
  • 1
  • 2
  • 2
    Possible duplicate of [How do I do a case-insensitive string comparison?](https://stackoverflow.com/questions/319426/how-do-i-do-a-case-insensitive-string-comparison) – MisterMiyagi Mar 08 '19 at 12:28

4 Answers4

5

Assuming all your city names are lowercase, you can use

city = input('Enter a city name').lower()

or .upper(), depending on how your csv is structured

vahdet
  • 6,357
  • 9
  • 51
  • 106
Merig
  • 1,751
  • 2
  • 13
  • 18
  • So to clarify I should have in my while loop: city = input('Would you like to filter the data for Chicago, New York City or Washington? \n').lower() – colm6287 Mar 08 '19 at 12:31
  • Yes. Using the `str.lower()` - that is, built-in method of the string. Manual: https://docs.python.org/3/library/stdtypes.html#str.lower Since the result of `input()` in your case will be a string, use the `.lower()` or `.upper()` method is clear. – s3n0 Mar 08 '19 at 13:32
  • Do I have to put .lower() after the input in the while loop as well e.g: while not city in valid_cities: city = input('Selection error! Please choose a vaild city \n').lower() – colm6287 Mar 08 '19 at 14:57
  • @colm6287 yes, everytime you ask for user input again – Merig Mar 08 '19 at 16:10
3

Just use:

input().lower()

And compare lowercase with lowercase

razimbres
  • 4,715
  • 5
  • 23
  • 50
  • 1
    So to clarify I should have in my while loop: city = input('Would you like to filter the data for Chicago, New York City or Washington? \n').lower() – colm6287 Mar 08 '19 at 12:33
  • Yes, exactly that. – razimbres Mar 08 '19 at 12:35
  • Do I have to put .lower() after the input in the while loop as well e.g: while not city in valid_cities: city = input('Selection error! Please choose a vaild city \n').lower() – colm6287 Mar 08 '19 at 14:58
2

You can use inputSTR.lower() for that. It's an in built function for strings.

inputSTR = 'HeLLo'
print(inputSTR.lower())
EddyG
  • 675
  • 4
  • 13
  • 1
    So to clarify I should have in my while loop: city = input('Would you like to filter the data for Chicago, New York City or Washington? \n').lower() – colm6287 Mar 08 '19 at 12:32
  • Do I have to put .lower() after the input in the while loop as well e.g: while not city in valid_cities: city = input('Selection error! Please choose a vaild city \n').lower() – colm6287 Mar 08 '19 at 14:58
2

Use input().lower both while comparing and getting values

Sanjeev S
  • 1,113
  • 2
  • 13
  • 33
  • Do I have to put .lower() after the input in the while loop as well e.g: while not city in valid_cities: city = input('Selection error! Please choose a vaild city \n').lower() – colm6287 Mar 08 '19 at 14:59