【问题标题】:Python: Having issue with printing out only 1 line in a for loopPython:在 for 循环中仅打印 1 行时遇到问题
【发布时间】:2021-03-07 08:18:10
【问题描述】:

因此,如果使用 userInput 在字典中找到键,我的代码会输出值。如果在字典的键中找不到 userInput,我想打印一条消息“疾病名称不存在”。我可以让它工作,但是,它会遍历整个列表并为 text.txt 的每一行重复“疾病不存在”

我不知道如何让它只打印一次。这是我的代码:

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

【问题讨论】:

  • 您的打印语句在您的循环中,每个循环将打印一次。
  • 哎呀等一下,我需要修复我的代码。我没有包含“疾病名称不存在”
  • 我不确定你为什么要循环,如果你有一种疾病要检查,为什么不直接使用in
  • 它是一个完整的疾病列表。因为第一个函数是通过我桌面上的一个文件,该文件有一个代码和疾病名称列表
  • 只需要检查查询是否与字典中的键匹配,如果匹配则吐出值

标签: python python-3.x dictionary for-loop if-statement


【解决方案1】:

我认为不是

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

你可以的

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

或者如果你喜欢单线

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

【讨论】:

  • 抱歉现在修复了,ps你应该更好地命名你的变量! “d”不是个好名字。
  • 它的问题是当你在函数中的某个地方看到'd'时,你必须四处搜索以找到'd'的创建位置并计算出它存储的内容。而很明显,“疾病”仅从单行来看就包含了疾病的字典。在永远不会增长的小功能中,它还不错。
  • 我完成了我的代码,但感谢您的帮助!
【解决方案2】:

我认为这可能会对你有所帮助我用这个例子证明了它 diseases={'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.')

【讨论】:

  • 严重缩进的 Python 代码是无效的 Python 代码。请修复代码的缩进。如果您在代码块中格式化代码时需要帮助,请参阅formatting help
【解决方案3】:

找到我的解决方案:

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): ")

【讨论】:

    猜你喜欢
    • 2022-01-21
    • 2016-08-08
    • 2019-09-06
    • 2016-09-12
    • 2016-04-23
    • 2020-06-23
    • 1970-01-01
    • 2023-03-06
    • 2023-04-04
    相关资源
    最近更新 更多