简体   繁体   中英

Python: Having issue with printing out only 1 line in a for loop

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()
     

I think instead of

for key, value in d.items():
    if query in key:
        print(value)
    else:
        print("Disease name does not exist.")

You could do

diseases = {'covid':'1223','flu':'1332'}
query = input("Choose your poison")
if query in diseases:
    print(diseases[query])
else:
    print("Disease name does not exist.")

or if you like one-liners

diseases = {'covid':'1223','flu':'1332'}
query = input("Choose your poison")
print (diseases[query] if query in diseases else "Disease Name does not Exist")

Found my solution:

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 == key:
             print(value)
             break

        if query != key:
            print("Disease name does not exist")

        
                
    
        query = input("Give disease name (q to quit): ")

i think this might help you i proved it with this example diseasses={'name_1':'ICD_10_2','name_2':'ICD_10_2'}

    while query != "q" :
    query = query.lower() # lowercase
    if query == "q":
            break
    if d.get(query)!= None:
        print(d.get(query))
    if  d.get(query)== None:
        print('Disease name does not exist.')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM