有没有办法根据python中的另一个def再次追加追加的消息?
我可以知道是否可以将我的第二个循环结果附加到2 def内的第一个循环结果中,并且对于类似的str只打印一次?这是我的脚本示例,它只是我制作的一个简单示例,因为我的原稿很复杂有没有办法根据python中的另一个def再次追加追加的消息?,python,Python,我可以知道是否可以将我的第二个循环结果附加到2 def内的第一个循环结果中,并且对于类似的str只打印一次?这是我的脚本示例,它只是我制作的一个简单示例,因为我的原稿很复杂 import os def check(a): des = [] file_path = "C:\\Users\\Downloads\\" + a + ".txt" file_path = "C:\\Users\\Downloads\\&
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
您需要将des定义为一个全局变量,这样它就不会丢失check函数之外的内容。 在附加到消息之前,还需要检查消息是否已经存在于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()
我看不到输出之间有任何差异…您好,我编辑了预期结果,谢谢。将
des
列表移动到selected
并将其作为参数传递到check
?每次调用check
…使用append
和not in
,您都在初始化des
。这可能会解决问题