首页 > 解决方案 > 使用 bakeshare 数据的假人 Python

问题描述

我想运行这段代码,但我有一些错误,我看不到问题。代码如下。我是否必须为城市、月份和日期设置全球列表?

import time
import pandas as pd
import numpy as np

CITY_DATA = { 'chicago': 'chicago.csv', 'new york city': 'new_york_city.csv',
              'washington': 'washington.csv' }


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 "all" to apply no month filter
(str) day - name of the day of week to filter by, or "all" to apply no day filter
"""
print('Hello! Let\'s explore some US bikeshare data!')

# get user input for city (chicago, new york city, washington). HINT: Use a while loop to handle invalid inputs
cities = ('Chicago', 'New York', 'Washington')
while True:
city = input('Which of these cities do you want to explore : Chicago, New York or Washington? \n> ').lower()
if city in cities:
break

# get user input for month (all, january, february, ... , june)
months = ['january', 'february', 'march', 'april', 'may', 'june']
month = get_user_input('Now you have to enter a month to get some months result) \n> ', months)

# get user input for day of week (all, monday, tuesday, ... sunday)
days = ['sunday', 'monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday' ]
day = get_user_input('Now you have to enter a month to get some months result) \n> ', days)

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

标签: pythonpandasnumpy

解决方案


使用 .lower() 很好,但是您应该更改cities列表和 `CITY_DATA` 字典以匹配名称(所以纽约市 --:纽约)。

CITY_DATA = { 'chicago': 'chicago.csv', 'new york': 'new_york_city.csv',
          'washington': 'washington.csv' }


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 "all" to apply no month filter
    (str) day - name of the day of week to filter by, or "all" to apply no day filter
    """
    print('Hello! Let\'s explore some US bikeshare data!')

    # get user input for city (chicago, new york city, washington). HINT: Use a while  loop to handle invalid inputs
    cities = ('chicago', 'new york', 'washington')
    while True:
        city = raw_input('Which of these cities do you want to explore : Chicago, New York or Washington? \n> ').lower()

        if city in cities:
            break

    # get user input for month (all, january, february, ... , june)
    months = ['january', 'february', 'march', 'april', 'may', 'june']
    month = raw_input('Now you have to enter a month to get some months result \n> {} \n>'.format(months)).lower()

    # get user input for day of week (all, monday, tuesday, ... sunday)
    days = ['sunday', 'monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday']
    day = raw_input('Now you have to enter a day to get some days result \n> {} \n>'.format(days)).lower()

    print('-'*40)

    if month == '' and day == '':
        return city, months, days
    elif month == '' and day != '':
        return city, months, day
    elif month != '' and day == '':
        return city, month, days
    else:
        return city, month, day


city, month, day = get_filters()

print(city, month, day) 

如果您使用的是 Python 2.7,则应raw_input()使用input().


推荐阅读