Python 每当我输入时,都会显示无效选项
我已经开始学习python编程。我正在完成交给我的项目。每当我输入时,都会显示无效选项。我无法解决这个问题。我不知道我是否出了问题。我知道的每件事我都试过了。请帮忙! 我的输出 python bikeshare.py 你好!!让我们来探索一些美国自行车用品数据! 请输入您想探索的城市编号: 芝加哥1号,纽约2号,华盛顿3号 一, 无效的选择。。。 请输入您想探索的城市编号: 芝加哥1号,纽约2号,华盛顿3号 谢谢你的帮助Python 每当我输入时,都会显示无效选项,python,Python,我已经开始学习python编程。我正在完成交给我的项目。每当我输入时,都会显示无效选项。我无法解决这个问题。我不知道我是否出了问题。我知道的每件事我都试过了。请帮忙! 我的输出 python bikeshare.py 你好!!让我们来探索一些美国自行车用品数据! 请输入您想探索的城市编号: 芝加哥1号,纽约2号,华盛顿3号 一, 无效的选择。。。 请输入您想探索的城市编号: 芝加哥1号,纽约2号,华盛顿3号 谢谢你的帮助 import time import pandas as pd impor
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()
导入时间
作为pd进口熊猫
将numpy作为np导入
芝加哥=‘芝加哥’
纽约市=‘纽约市’
华盛顿=‘华盛顿’
CITY_DATA={CHICAGO:'CHICAGO.csv',
纽约:纽约市.csv,
华盛顿:'华盛顿.csv'}
每周的天数=[
“星期一”,
"星期二",,
“星期三”,
"星期四",,
“星期五”,
“星期六”,
“星期日”]
年的月=[
“一月”,
“二月”,
“三月”,
"四月",,
“五月”,
“六月”,
“七月”,
“八月”,
"九月",,
“十月”,
"十一月",,
“12月”]
小时=[
“上午12点”
"凌晨一时",,
"凌晨二时",,
"凌晨三时",,
"凌晨四时",,
"上午五时",,
"上午六时",,
"上午七时",,
"上午八时",,
‘上午九时’,
"上午十时",,
"上午十一时",,
‘下午十二时’,
下午一时,,
"下午二时",,
"下午三时",,
"下午四时",,
"下午五时",,
"下午六时",,
"晚上七时",,
"晚上八时",,
"晚上九时",,
"晚上十时",,
“晚上11点”
]
分钟/秒=60
小时秒=60*分钟秒
日秒=24小时秒
周秒=7天秒
#打印(城市数据)
开始时间='开始时间'
结束时间='结束时间'
出生年=‘出生年’
START_站=‘START站’
END_站=‘END站’
行程持续时间=‘行程持续时间’
性别=‘性别’
#新增栏目
开始月份='开始月份'
每周开始日='每周开始日'
def get_过滤器():
"""
要求用户指定要分析的城市、月份和日期。
返回:
(str)城市-要分析的城市名称
(int)month-要筛选的月份名称,或“全部”不应用月份筛选
(int)day-要筛选的星期的名称,或“all”表示不应用日期筛选
"""
无效的选择=“无效的选择…”
ALL='ALL'
print('您好!让我们来看看一些美国自行车用品数据!')
#获取城市(芝加哥、纽约、华盛顿)的用户输入。提示:使用while循环处理无效输入
尽管如此:
打印(“请输入您想探索的城市编号:”)
印刷品(“芝加哥1号,纽约2号,华盛顿3号”)
位置=输入(“>”)
如果位置==“1”:
城市=芝加哥
打印(“您选择了芝加哥”)
打破
elif位置==“2”:
城市=纽约
打印(“您选择了纽约市”)
打破
elif位置==“3”:
城市=华盛顿
打印(“您选择了华盛顿”)
打破
其他:
打印(无效的_选项)
尽管如此:
get_city=input('\nHello!让我们浏览一些美国自行车用品数据!\n'
'您想查看芝加哥、纽约或华盛顿的数据吗?\n')
如果在('chicago'、'new york'、'washington')中获得_city.lower():
如果get_city.lower()==“芝加哥”:
城市名称=芝加哥
elif get_city.lower()=“纽约”:
城市\文件名=纽约\城市
elif get_city.lower()=“华盛顿”:
城市=华盛顿
打破
打印('输入选项中提供的有效城市名称')
尽管如此:
打印(“请输入您要浏览的(开始)月份的编号或\“{}\:”。格式(全部))
打印(“1月1日至6月6日”)
m=输入(“>”)
如果m==全部:
月份=无
打破
尝试:
月份=整数(m)
除值错误外:
打印(无效的_选项)
持续
其他:
如果月>=1,月=1,天代码与我一起工作。。。你是怎么运作的
您的比较是以字符串形式查找数字。如果您使用标准输入(例如,在终端中运行),那么它将以字符串的形式读入。但是,如果您以某种方式传入一个整数,那么它将不匹配
neil~$python stack.py
你好让我们来探索一些美国自行车用品数据!
请输入您想探索的城市编号:
芝加哥1号,纽约2号,华盛顿3号
一,
你选择了芝加哥
你好!!让我们来探索一些美国自行车用品数据!
您想查看芝加哥、纽约或华盛顿的数据吗?您正在运行哪个版本的Python?您的代码看起来像Python,但也像是在Python 2中运行。然而,input
函数在这两个版本中的行为有所不同