def run_country_checker(): """Checks for a valid country by checking df""" while True: try: country = input( text_color( "What is your country? ", text_type.QUESTION, ), ) country = country.lower() country = country.title() float(df[df.country == country]["purchasing_power_index"]) except TypeError: print( text_color( f"'{country}' is an invalid country. Please try again.", text_type.WARNING, ), ) else: return country
def run_gender_checker(): """Take valid gender from user""" while True: try: gender = input( text_color( "What is your gender Male or Female? ", text_type.QUESTION, ), ) gender = gender.lower() if gender not in ["male", "female"]: raise ValueError except ValueError: print( text_color( f"'{gender}' is an invalid Gender. Please try again", text_type.WARNING, ), ) else: return gender
def run_country_checker(): """Checks for a valid country by checking df""" while True: try: country = input( text_color( "What is your country? ", text_type.QUESTION, ), ) country = country.lower() country = country.title() float(df[df.country == country]["purchasing_power_index"]) except TypeError: ret = get_closest_country(country) error_str = f"{country} is an invalid country or did you mean {ret}. Please try again." if not ret: error_str = f"{country} is an invalid country name. Please try again." print(text_color(error_str, text_type.WARNING)) else: return country
def safety_func(): """finds your safety index""" country_safety_index = float( df[df.country == YOUR_COUNTRY]["safety_index"], ) print( text_color( f"In your country safety index is {country_safety_index}", text_type.ANSWER, ), ) while True: index_input = (input( print_question('safety index', max_min_safety, 'higher is better'), )) if isinstance(_value_checker(index_input), float): return _value_checker(index_input) elif _value_checker(index_input) == "default": return country_safety_index print( text_color( f"'{index_input}' is an invalid index. Please try again.", text_type.WARNING, ), )
def pollution_func(): """finds your pollution index""" country_pollution_index = float( df[df.country == YOUR_COUNTRY]["pollution_index"]) print( text_color( f"In your country pollution index is {country_pollution_index}", text_type.ANSWER, ), ) while True: index_input = input( print_question("pollution index", max_min_pollution, "lower is better"), ) if isinstance(_value_checker(index_input), float): return _value_checker(index_input) elif _value_checker(index_input) == "default": return country_pollution_index print( text_color( f"'{index_input}' is an invalid index. Please try again.", text_type.WARNING, ), )
def format_info(column_title, value): """[summary] Arguments: column_title {[type]} -- [description] value {[type]} -- [description] Returns: [type] -- [description] """ return text_color( f"In your country {column_title} is {value}", text_type.ANSWER, )
def run_country_checker(): """Checks for a valid country by checking df""" while True: try: country = input( text_color("What is your country? ", text_type.QUESTION)) country = country.lower() country = country.title() float(df[df.country == country]["purchasing_power_index"]) except TypeError: # ret = get_closest_country(country) # error_str = f"{country} is an invalid country or did you mean {ret}. Please try again." # if not ret: # error_str = f"{country} is an invalid country name. Please try again." # print(text_color(error_str, text_type.WARNING)) similarCountries = [] for i in range(len(df)): if (isSimilarTo(df['country'][i], country)): similarCountries.append(df['country'][i]) if (len(similarCountries) == 0): message = f"'{country} is an invalid country. Please try again." else: message = f"{country} is an invalid country. Did you mean " for i in range(len(similarCountries)): message += f"{similarCountries[i]}" if (i != len(similarCountries) - 1): message += " or " else: message += "?" print(text_color(message, text_type.WARNING)) else: return country
def health_care_func(): """finds your health care index""" country_health_care_index = float( df[df.country == YOUR_COUNTRY]["health_care_index"], ) print( text_color( f"In your country health care index is {country_health_care_index}", text_type.ANSWER, ), ) while True: index_input = input( print_question("health care index", max_min_health, mood="higher is better"), ) if isinstance(_value_checker(index_input), float): return _value_checker(index_input) elif _value_checker(index_input) == "default": return country_health_care_index print( text_color(f"'{index_input}' is an invalid index. Please try again."), text_type.WARNING, )
def ask_user_country(self): """Checks for a valid country by checking df.""" while True: country_name = input( text_color("What is your country? ", text_type.QUESTION), ) country_name = country_name.title() try: float( self.countries_df[self.countries_df.country == country_name]["purchasing_power_index"], ) except TypeError: closest_country = self.get_closest_country(country_name) error_str = f"{country_name} is an invalid country or did you mean {closest_country}. Please try again." if not closest_country: error_str = ( f"{country_name} is an invalid country name. Please try again." ) print(text_color(error_str, text_type.WARNING)) else: return country_name
def print_question(name_index, max_min_value, mood): """ Return a question that asks the user about the status of a factor in his country, also displays the maximum and minimum value of this factor in the world. """ if mood == 'higher is better': first_value = max_min_value[0] second_value = max_min_value[1] elif mood == 'lower is better': first_value = max_min_value[1] second_value = max_min_value[0] message = text_color( f"What is your desirable {name_index} ({mood})? " f"The best score in the world is " f"{first_value[0]} " f"({first_value[1]}), " f"the worst is {second_value[0]} " f"({second_value[1]}) ", text_type.QUESTION, ) return message
def format_question(name_index, max_min_value, mood): """Return a question that asks the user about the status of a factor in his country. Also displays the maximum and minimum value of this factor in the world. """ if mood == "lower is better": max_min_value = max_min_value[::-1] best_value, best_value_country_name = max_min_value[0] worst_value, worst_value_country_name = max_min_value[1] message = text_color( f"What is your desirable {name_index} ({mood})? " f"The best score in the world is " f"{best_value} " f"({best_value_country_name}), " f"the worst is {worst_value} " f"({worst_value_country_name}) ", text_type.QUESTION, ) return message
your_cost_of_living_index = float(cost_of_living_func()) your_property_price_to_income_ratio = float( property_price_to_income_ratio_func(), ) your_traffic_commute_time_index = float(traffic_commute_time_func()) your_pollution_index = float(pollution_func()) out_df = df[(df.purchasing_power_index > your_purchasing_power_index) & (df.safety_index > your_safety_index) & (df.health_care_index > your_health_care_index) & (df.cost_of_living_index < your_cost_of_living_index) & (df.property_price_to_income_ratio < your_property_price_to_income_ratio) & (df.traffic_commute_time_index < your_traffic_commute_time_index) & (df.pollution_index < your_pollution_index) & (df.climate_index > your_climate_index)] print_out_df = out_df[[ "country", "freedomhouse_score", "quality_of_life_index" ]].dropna().sort_values(by=['freedomhouse_score'], ascending=False) if print_out_df.empty: print( text_color(f"There is no country better than {YOUR_COUNTRY}.", ), text_type.ANSWER, ) else: with pd.option_context("display.max_rows", None, "display.max_columns", None): print(text_color(print_out_df, text_type.ANSWER))
desired: Dict[str, float] = PLACE2LIVE.desired_indexes OUT_DF = DF[(DF.purchasing_power_index > desired["purchasing_power_index"]) & (DF.safety_index > desired["safety_index"]) & (DF.health_care_index > desired["health_care_index"]) & (DF.cost_of_living_index < desired["cost_of_living_index"]) & (DF.property_price_to_income_ratio < desired["property_price_to_income_ratio"]) & (DF.traffic_commute_time_index < desired["traffic_commute_time_index"]) & (DF.pollution_index < desired["pollution_index"]) & (DF.climate_index > desired["climate_index"])] PRINT_OUT_DF = (OUT_DF[[ "country", "freedomhouse_score", "quality_of_life_index" ]].dropna().sort_values(by=["freedomhouse_score"], ascending=False)) if PRINT_OUT_DF.empty: print( text_color( "There is no country better than {}.".format( PLACE2LIVE.user_country_name), text_type.ANSWER, ), ) else: with pd.option_context("display.max_rows", None, "display.max_columns", None): print( text_color(PRINT_OUT_DF.to_string(index=False), text_type.ANSWER))