So my code outputs the value if the keys are found inside a dictionary using a userInput. I want to print out a message saying "Disease name does not exist" if the userInput is not found in the keys of the dictionary. I can get it to work, however, it goes through the wholeee list and repeats "Disease does not exist" for every line of the text.txt
I can't figure out how to make it just print once. Here is my code:
# Complete this function to meet its specifications.
# Begin with an empty dictionary, fill it, and return it.
def disease_to_code_dictionary( ) :
""" Function returns a dictionary with disease names as keys and
ICD 10 codes as values. """
diseases = {}
infile = open("ICD10.txt","r")
header_row = infile.readline() # skip the header row
for line in infile :
cells = line.split("\t") # split by the tab character
if len(cells) >= 2 : # only if the line had a tab
code = cells[0]
disease = cells[1]
disease = disease.lower() # lowercase
disease = disease.replace("\"","") # remove all double quotes
diseases[disease] = code
infile.close()
return diseases
# Complete this function to meet its specifications.
# The program should give the code if the disease name exists
# otherwise say "Disease name does not exist.".
def query_disease_to_code() :
""" Interactive function to query code from disease name. """
d = disease_to_code_dictionary() # disease to code dictionary
query = input("Give disease name (q to quit): ")
while query != "q" :
query = query.lower() # lowercase
# complete here
for key, value in d.items():
if query in key:
print(value)
else:
print("Disease name does not exist.")
query = input("Give disease name (q to quit): ")
query_disease_to_code()
Aucun commentaire:
Enregistrer un commentaire