【问题标题】:Python for dummies using the bakeshare data使用 bakeshare 数据的假人 Python
【发布时间】:2019-04-04 18:46:50
【问题描述】:

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

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

【问题讨论】:

  • 您能否添加一些有关您遇到哪些错误的信息?这有助于发现问题。

标签: python pandas numpy


【解决方案1】:

使用 .lower() 很好,但您应该更改您的 cities 列表和您的 `CITY_DATA` 字典以匹配名称(因此 New York City --: new york)。

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()

【讨论】:

    【解决方案2】:

    如果我理解正确,我认为这就是你必须做的:

    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> ').capitalize() #was lower()
            if city in cities:
                break
    
        # get user input for month (all, january, february, ... , june)
        months = ['january', 'february', 'march', 'april', 'may', 'june']
        month = input('Now you have to enter a month to get some months result \n> {} \n> '.format(months))
    
        # get user input for day of week (all, monday, tuesday, ... sunday)
        days = ['sunday', 'monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday']
        day = input('Now you have to enter a dau to get some days result \n> {} \n> '.format(days))
    
        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)
    

    有几点需要注意: 城市的输入设置为“.lower()”,而城市列表包含大写字母。所以我把它改成了capitilize()。

    如果用户没有指定任何输入,您还希望它每天和每月返回。因此,我在最后添加了一个简单的 if-test 来检查它。此外,我使用“-format()”来显示用户在输入中的不同选项。希望一切都清楚!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-30
      • 1970-01-01
      • 2021-11-12
      • 1970-01-01
      相关资源
      最近更新 更多