python - 当我输入时,它说无效选项
问题描述
我已经开始学习 python 编程。我正在制作给我的项目。当我输入时,它会说无效的选项。我无法解决问题。我不知道我错了。我已经尝试了我所知道的一切。请帮忙 !我的输出 python bikeshare.py
你好!让我们探索一些美国共享单车数据!请输入您想探索的城市编号:1 芝加哥、2 纽约市、3 华盛顿
1 无效选择... 请输入您想探索的城市编号:1 芝加哥、2 纽约市、3 华盛顿
感谢您的帮助
import time
import pandas as pd
import numpy as np
CHICAGO = 'Chicago'
NYC = 'New York City'
WASHINGTON = 'Washington'
CITY_DATA = { CHICAGO: 'chicago.csv',
NYC: 'new_york_city.csv',
WASHINGTON: 'washington.csv' }
DAYS_OF_WEEK = [
'Monday',
'Tuesday',
'Wednesday',
'Thursday',
'Friday',
'Saturday',
'Sunday']
MONTHS_OF_YEAR = [
'January',
'February',
'March',
'April',
'May',
'June',
'July',
'August',
'September',
'October',
'November',
'December']
HOURS = [
'12 AM'
'1 AM',
'2 AM',
'3 AM',
'4 AM',
'5 AM',
'6 AM',
'7 AM',
'8 AM',
'9 AM',
'10 AM',
'11 AM',
'12 PM',
'1 PM',
'2 PM',
'3 PM',
'4 PM',
'5 PM',
'6 PM',
'7 PM',
'8 PM',
'9 PM',
'10 PM',
'11 PM'
]
MINUTE_SEC = 60
HOUR_SEC = 60 * MINUTE_SEC
DAY_SEC = 24 * HOUR_SEC
WEEK_SEC = 7 * DAY_SEC
# print(CITY_DATA)
START_TIME = 'Start Time'
END_TIME = 'End Time'
BIRTH_YEAR = 'Birth Year'
START_STATION = 'Start Station'
END_STATION = 'End Station'
TRIP_DURATION = 'Trip Duration'
GENDER = 'Gender'
# Added columns
START_MONTH = 'Start Month'
START_DAY_OF_WEEK = 'Start Day of Week'
def get_filters():
"""
Asks user to specify a city, month, and day to analyze.
Returns:
(str) city - name of the city to analyze
(int) month - name of the month to filter by, or "all" to apply no month filter
(int) day - name of the day of week to filter by, or "all" to apply no day filter
"""
invalid_choice = "Invalid choice..."
ALL = 'all'
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
while True:
print("Please enter the number of which city you would like to explore: ")
print("1 Chicago, 2 New York City, 3 Washington")
location = input(">")
if location == '1':
city = CHICAGO
print("You chose Chicago")
break
elif location == '2':
city = NYC
print("You chose New York City")
break
elif location == '3':
city = WASHINGTON
print("You chose Washington")
break
else:
print(invalid_choice)
while True:
get_city = input('\nHello! Let\'s explore some US bikeshare data!\n'
'Would you like to see data for Chicago, New York, or Washington?\n')
if get_city.lower() in ('chicago', 'new york', 'washington'):
if get_city.lower() == 'chicago':
city_filename = chicago
elif get_city.lower() == 'new york':
city_filename = new_york_city
elif get_city.lower() == 'washington':
city_filename = washington
break
print('Enter a valid city name provided in the options')
while True:
print("Please enter the number of the (start) month you would like to explore or \"{}\": ".format(ALL))
print("1 January ... 6 June")
m = input(">")
if m == ALL:
month = None
break
try:
month = int(m)
except ValueError:
print(invalid_choice)
continue
else:
if month >= 1 and month<=6:
print("You chose " + MONTHS_OF_YEAR[month-1])
break
elif month <= 12:
print("Only January to June are in this dataset")
print(invalid_choice)
continue
else:
print(invalid_choice)
continue
# get user input for day of week (all, monday, tuesday, ... sunday)
while True:
print("Please enter the number of the (start) day of the week that you would like to explore or \"{}\": ".format(ALL))
print("1 Monday ... 7 Sunday")
d = input("> ")
if d == ALL:
day = None
break
try:
day = int(d)
except ValueError:
print(invalid_choice)
continue
else:
if day >= 1 and day<=7:
day -= 1
print("You chose " + DAYS_OF_WEEK[day])
break
else:
print(invalid_choice)
continue
print_divider()
return city, month, day
def main():
while True:
city, month, day = get_filters()
df = load_data(city, month, day)
time_stats(df, month is None, day is None)
station_stats(df)
trip_duration_stats(df)
user_stats(df)
trip_length_time_of_day_correlation(df)
restart = input('\nWould you like to restart? Enter yes or no.\n')
if restart.lower() != 'yes':
print("Bye!")
break
if __name__ == "__main__":
main()
解决方案
该代码与我一起使用...您如何运行它?
您的比较正在寻找作为字符串的数字。如果您使用标准输入(例如,在终端中运行),那么它将作为字符串读入。但是,如果你以某种方式传入一个整数,那么它将不匹配。
尼尔~$ python stack.py 你好!让我们探索一些美国共享单车数据!请输入您想探索的城市编号:1 芝加哥、2 纽约市、3 华盛顿
1 你选择了芝加哥
你好!让我们探索一些美国共享单车数据!您想查看芝加哥、纽约或华盛顿的数据吗?
推荐阅读
- javascript - 如何将嵌套对象的层次结构转换为自身的平面表示,每个平面键反映其原始属性路径?
- algorithm - 在 k 天内移动 N 个项目 ((N>=k)
- python - Spyder 5.0.5 (MacOS) 上没有 Spyder 终端
- robotframework - 如何在 Robot Framework 的测试用例中导入资源文件?
- go - 无法从同一目录中的文件导入函数
- performance - 启用 API 级别的诊断日志 成本和性能是什么
- django - 直接显示来自 Digitalocean 应用平台的媒体图像
- reactjs - Next.js getStaticProps localstorage 未定义
- scenekit - 在世界空间中旋转和移动 SCNNode
- python - 如何获取当前运行的子进程、他的父进程和父父进程的 PID、真实用户 ID、优先级