python - 有没有办法根据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
解决方案
您需要将 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()
推荐阅读
- python - 将代理与 PyPaperBot 模块一起使用时出错
- lombok - 使用 @AllArgsConstructor 注释的类的对象
- python - “RuntimeError: <_overlapped.Overlapped object> 在释放时仍有待处理的操作”同时使用 aiohttp
- javascript - Discord JS - DiscordAPIError:缺少访问权限
- sed - 使用 sed 将 ' 字符替换为 \'
- github - 为什么协作者的拉取请求上没有运行操作/工作流程?
- r - 在R中使用For循环重命名具有索引的列
- python - 如何制作 if 语句来检查列表中的所有三个项目是否都存在于另一个列表中
- amazon-web-services - Govcloud 私有托管区域中的 ACM 待验证
- php - 非法字符串偏移 'url' Wordpress