Search code examples
pythonpython-3.xkeyerror

How can I work around a KeyError in Python?


I am trying to create a Forecasting tool, which analyses historical passenger traffic at a given airport. The anylysis will be based on a linear regression with various GDPs (Gross Domestic Product) of countries related to the airport.

A person can type in the name of the independant variable, which then gets selected from the Excel file.

Once a person gets the question "Which Country's GDP would you like to set as the independant variable for the Regression Analysis?", there is the possibility of typing a country wrong. In that case I receive a KeyError.

I am trying to work around that with "try / except", but I still receive a KeyError (See lines 36-49). I would really appreciate some help!

Thank you!

If it helps, here is the GitHub Link: https://github.com/DR7777/snowflake (See lines 36-49 of main_file.py)

Here is my code:

Ive tried with while loops, for / except, but it seems I am too new to understand.


# This part saves the first row of the Excel as a list,
# so I can give the user a list of all the countries, 
# if the person types in a country, that's not on the list.

loc = ("IMF_Country_GDP_Data.xlsx") 
wb = xlrd.open_workbook(loc) 
sheet = wb.sheet_by_index(0) 
sheet.cell_value(0, 0) 
list_of_countries = sheet.row_values(0)

possible_selection = (list_of_countries[1:]) #This is the list with all the possible countries, without the Excel cell A1


#Introduction

print("Hello, welcome to the Air Traffic Forecasting Tool V0.1!")
print("Which Country's GDP would you like to set as the independant variable for the Regression Analysis?")
Country_GDP = input("Please type your answer here: ")



#here we check, if the typed Country is in the list


try:
    possible_selection == Country_GDP
    print("Your country is on the list.")

except KeyError:
    print("Oh no! You typed a country, which is not in our database!")
    print("Please select one of the countries listed below and try again")
    print(possible_selection)


#now continuing with the previous code


print("Ok, I will conduct the analysis based on the GDP of " + str(Country_GDP) + "!")
print("Here are your results: ")


#here is the rest of the code



What I want to achieve is: If a person types a name, which is on the list of countries, the program runs the regression.

If the country is not on the list, I dont want to receive a KeyError. I would like the program to say: Oh no! You typed a country, which is not in our database! Please select one of the countries listed below and try again And then print the possible_selection variable, so the user can see which selection he has.

Thank you very much!


Solution

  • No need to get a key error at all. Just use in.

    while True:
        selection = input('Which country?')
        if selection in list_of_countries: 
            print('Your country is on the list')
            break
        else:
            print('You typed an invalid entry, lets try again')