首页 > 解决方案 > 有没有办法根据python中的另一个def再次附加附加消息?

问题描述

我可以知道是否可以将我的第二个循环结果附加到 2 def 内的第一个循环结果并为类似的 str 只打印一次?这是我的脚本示例,这只是我制作的一个简单示例,因为我的原件很复杂。

import os

def check(a):
    des = [] 
    
    file_path = "C:\\Users\\Downloads\\" + a + ".txt"
    file_path = "C:\\Users\\Downloads\\" + a + ".txt"
    file_path2 = "C:\\Users\\Document\\" + a + ".txt"
    file_path3 = "C:\\Users\\Picture\\" + a + ".txt"
    file_path4 = "C:\\Users\\Downloads\\" + a + "22-Oct"+ ".txt"
    
    if os.path.exists(file_path):
        return file_path
    else:
        des.append("file not exists in downloads.")
    
    if os.path.exists(file_path2):
        return file_path
    else:
        des.append("file not exists in document.")
           
    if os.path.exists(file_path3):
        return file_path
    else:
        des.append("file not exists in picture.")

    if os.path.exists(file_path4):
        return file_path
    else:
        des.append(a + " 22-Oct file not eexists.")
        
    if len(des) != 0:   
        for empty in des:
            print(empty)
                
    elif len(des) == 0:
        print('yes') # going to add another def if true
     
def selected():
    while True:
        a = input('Please select temp or 1456.\n')
        
        if a == "temp" or a == "1456":
            check(a)
        else:
            print("please select again.")

selected()

我得到的结果是

OUTPUT:

Please select temp or 1456.
temp
file not exists in downloads.
file not exists in document.
file not exists in picture.
temp 22-Oct file not exists.

Please select temp or 1456.
1456
file not exists in downloads.
file not exists in document.
file not exists in picture.
1456 22-Oct file not exists.
:
:

预期结果是第一个和第二个的组合,然后只显示一次。

Please select temp or 1456.
temp
file not exists in downloads.
file not exists in document.
file not exists in picture.
temp 22-Oct file not exists.

Please select temp or 1456.
1456
file not exists in downloads. ## result from both (shows only once)
file not exists in document.  ## result from both (shows only once)
file not exists in picture.  ## result from both (shows only once)
temp 22-Oct file not exists. ## result from first loop 
1456 22-Oct file not exists. ## result from second loop

标签: python

解决方案


您需要将 des 定义为全局变量,这样它就不会在检查函数之外丢失其内容。您还需要在附加消息之前检查该消息是否已存在于 des 中。以下是您可以重复使用的示例。注意 des 声明的位置。我们不能在检查中检查它,否则它每次都会被初始化。

def check(a):
    file_path = "C:\\Users\\Downloads\\" + a + ".txt"
    file_path = "C:\\Users\\Downloads\\" + a + ".txt"
    file_path2 = "C:\\Users\\Document\\" + a + ".txt"
    file_path3 = "C:\\Users\\Picture\\" + a + ".txt"
    file_path4 = "C:\\Users\\Downloads\\" + a + "22-Oct"+ ".txt"
    
    if "file not exists in downloads." not in des:
        des.append("file not exists in downloads.")
    if "file not exists in downloads." not in des:
        des.append("file not exists in document.")
    if "file not exists in downloads." not in des:
        des.append("file not exists in picture.")
    if (a + " 22-Oct file not exists") not in des:
        des.append(a + " 22-Oct file not exists.")
        
    if len(des) != 0:   
        for empty in des:
            print(empty)
                
    elif len(des) == 0:
        print('yes') # going to add another def if true
     
def selected():
    global des
    des = [] 
    while True:
        a = input('Please select temp or 1456.\n')
        
        if a == "temp" or a == "1456":
            check(a)
        else:
            print("please select again.")

selected()

推荐阅读