the problem is that After choosing the name of the city, the code is freezing , he code is:
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!')
# TO DO: get user input for city (chicago, new york city, washington). HINT: Use a while loop to handle invalid inputs
city = input( "please choose a city from (chicago , new york city , washington): ").lower()
while True:
if city not in CITY_DATA.keys():
print("invaild city name please try again/n: ")
city = input( "please choose a city from (chicago , new york city , washington): ").lower()
break
# TO DO: get user input for month (all, january, february, ... , june)
month = input(" please choose and type a full month name or type all: ").lower()
months = ['january' , 'faburay' , 'march' , 'april' , 'may' , 'june' , 'all' ]
while True:
if month not in months:
print("invaild month name please try again")
month = input(" please choose and type a full month name or type all: ").lower()
break
# TO DO: get user input for day of week (all, monday, tuesday, ... sunday)
day = input("please add a week day name or type all: ").lower()
days = ['saturday', ' sunday', 'monday' , 'tusday', 'wedensday','thrusday','friday','all']
while True:
if day not in days:
prtint('invaild week day name please try again')
day = input("please add a week day name or type all: ").lower()
break
print('-'*40)
return city, month, day
it was WORKING AT FIRST BUT IT SUDDINLY BROKE and i cant make sure that the rest of the code is working since its not working from the beginning, the project is all about bikeshare data that should return specific statistics when choosing specific city , month , and day
CodePudding user response:
One easy fix would be to add the break
in an else block (Though not the best solution this will get you going):
while True:
if city not in CITY_DATA.keys():
print("invaild city name please try again/n: ")
city = input( "please choose a city from (chicago , new york city , washington): ").lower()
else:
break
This is because you ask for the input then enter the while, if the city is not correct then the user gets a new input change, after that there is a break even if that answer is incorrect.
Adding the else
means only breaking if if city not in CITY_DATA.keys():
is false (The city is in the keys)
Like i said this is maybe not the optimal solution and you should read more into the control flow of the code
Here is a working example of making the code a little easier to work with and not repeat the while several times:
import time
from enum import Enum
CITY_DATA = { 'chicago': 'chicago.csv',
'new york city': 'new_york_city.csv',
'washington': 'washington.csv' }
MONTHS = ['january' , 'faburay' , 'march' , 'april' , 'may' , 'june' , 'all' ]
DAYS = ['saturday', ' sunday', 'monday' , 'tusday', 'wedensday','thrusday','friday','all']
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
"""
global CITY_DATA, MONTHS, DAYS
print('Hello! Let\'s explore some US bikeshare data!')
# TO DO: get user input for city (chicago, new york city, washington). HINT: Use a while loop to handle invalid inputs
city = get_input(CITY_DATA, "please choose a city from (chicago , new york city , washington): ", "invaild city name please try again/n: ")
# TO DO: get user input for month (all, january, february, ... , june)
month = get_input(MONTHS, " please choose and type a full month name or type all: ", "invaild month name please try again/n:")
# TO DO: get user input for day of week (all, monday, tuesday, ... sunday)
day = get_input(DAYS, "please add a week day name or type all: ", 'invaild week day name please try again/n:')
print('-'*40)
return city, month, day
def get_input(correct_list, input_text: str, error_text: str) -> str:
"""Get the input and return the input if it's in the correct_list
Args:
correct_list (list): The list of correct inputs
input_text (str): The text to show the user before listening for an input
error_text (str): The error text to show if the user enters something not in the correct_list
Returns:
str: The string entered if in correct_list
"""
output = ""
while True:
output = input(input_text).lower()
if output not in DAYS:
print(error_text)
else:
return output
if __name__ == "__main__":
get_filters()