运行 相同的程序直到满足条件
Running the same program until condition satisfied
我正在尝试创建一个小程序来搜索图像文件夹,选择一个,检查其大小并在所选图像至少为 5KB 时完成。如果不是,那么我需要它循环回到选择步骤(然后是尺寸检查,等等)
我正在使用函数来选择和检查大小,但是当我尝试在 while 循环中使用它们时,我遇到了各种缩进错误,现在我很困惑。我已经评论了我使用该功能的部分,但实际上我想我想让整个事情循环回到顶部的评论......
这是我的代码 -
#CHOOSE POINT
def chosen():
random.choice(os.listdir(r"/Users/me/p1/images"))
def size():
os.path.getsize(r"/Users/me/p1/images/"+chosen)
thresh = 5000
while size < thresh:
print(chosen + " is too small")
# loop back to CHOOSE POINT
else:
print(chosen + " is at least 5KB")
我是不是想错了?在我的 while 循环中使用函数会做我想做的事吗?实现我想要做的事情的最佳方式是什么?我对此很陌生并且很困惑。
首先要意识到的是这样的代码:
def chosen():
random.choice(os.listdir(r"/Users/me/p1/images"))
只是一个函数的定义。它只会在您每次实际调用它时运行,使用 chosen()
.
其次,random.choice()
将从提供的列表中随机选择(尽管每次调用时都从磁盘读取它是相当低效的,而且不清楚为什么要随机选择一个,但这没关系),但是由于您实际上 return
该值,因此该功能不是很有用。做出选择,然后放弃。相反,您可能想要:
def chosen():
return random.choice(os.listdir(r"/Users/me/p1/images"))
三、这个函数定义:
def size():
os.path.getsize(r"/Users/me/p1/images/"+chosen)
它尝试使用 chosen
,但这只是您之前定义的函数的名称。您可能想要获取所选的实际文件的大小,函数需要将其作为参数提供:
def size(fn):
return os.path.getsize(r"/Users/me/p1/images/"+fn)
现在要使用这些功能:
file_size = 0
threshold = 5000
while file_size < threshold:
a_file = chosen()
file_size = size(a_file)
if file_size < threshold:
print(a_file + " is too small")
else:
print(a_file + " is at least 5KB")
print('Done')
变量file_size
被初始化为0,以确保循环开始。循环将继续进行,直到满足开始时的条件。
每次执行chosen()
时,返回值都会被记住为变量a_file
,你可以在后面的代码中使用它来引用。
然后传递给 size()
,以获得大小,最后执行测试以打印正确的消息。
实现相同目标的更有效方法:
threshold = 5000
while True:
a_file = chosen()
file_size = size(a_file)
if file_size < threshold:
print(a_file + " is too small")
else:
break
print(a_file + " is at least 5KB")
break
刚刚退出 while
循环,该循环会一直运行下去,因为它会测试 True
。这样可以避免对同一件事进行两次测试。
所以,你最终会得到:
import random
import os
def chosen():
return random.choice(os.listdir(r"/Users/me/p1/images/"))
def size(fn):
return os.path.getsize(r"/Users/me/p1/images/"+fn)
threshold = 5000
while True:
a_file = chosen()
file_size = size(a_file)
if file_size < threshold:
print(a_file + " is too small")
else:
break
print(a_file + " is at least 5KB")
我正在尝试创建一个小程序来搜索图像文件夹,选择一个,检查其大小并在所选图像至少为 5KB 时完成。如果不是,那么我需要它循环回到选择步骤(然后是尺寸检查,等等)
我正在使用函数来选择和检查大小,但是当我尝试在 while 循环中使用它们时,我遇到了各种缩进错误,现在我很困惑。我已经评论了我使用该功能的部分,但实际上我想我想让整个事情循环回到顶部的评论...... 这是我的代码 -
#CHOOSE POINT
def chosen():
random.choice(os.listdir(r"/Users/me/p1/images"))
def size():
os.path.getsize(r"/Users/me/p1/images/"+chosen)
thresh = 5000
while size < thresh:
print(chosen + " is too small")
# loop back to CHOOSE POINT
else:
print(chosen + " is at least 5KB")
我是不是想错了?在我的 while 循环中使用函数会做我想做的事吗?实现我想要做的事情的最佳方式是什么?我对此很陌生并且很困惑。
首先要意识到的是这样的代码:
def chosen():
random.choice(os.listdir(r"/Users/me/p1/images"))
只是一个函数的定义。它只会在您每次实际调用它时运行,使用 chosen()
.
其次,random.choice()
将从提供的列表中随机选择(尽管每次调用时都从磁盘读取它是相当低效的,而且不清楚为什么要随机选择一个,但这没关系),但是由于您实际上 return
该值,因此该功能不是很有用。做出选择,然后放弃。相反,您可能想要:
def chosen():
return random.choice(os.listdir(r"/Users/me/p1/images"))
三、这个函数定义:
def size():
os.path.getsize(r"/Users/me/p1/images/"+chosen)
它尝试使用 chosen
,但这只是您之前定义的函数的名称。您可能想要获取所选的实际文件的大小,函数需要将其作为参数提供:
def size(fn):
return os.path.getsize(r"/Users/me/p1/images/"+fn)
现在要使用这些功能:
file_size = 0
threshold = 5000
while file_size < threshold:
a_file = chosen()
file_size = size(a_file)
if file_size < threshold:
print(a_file + " is too small")
else:
print(a_file + " is at least 5KB")
print('Done')
变量file_size
被初始化为0,以确保循环开始。循环将继续进行,直到满足开始时的条件。
每次执行chosen()
时,返回值都会被记住为变量a_file
,你可以在后面的代码中使用它来引用。
然后传递给 size()
,以获得大小,最后执行测试以打印正确的消息。
实现相同目标的更有效方法:
threshold = 5000
while True:
a_file = chosen()
file_size = size(a_file)
if file_size < threshold:
print(a_file + " is too small")
else:
break
print(a_file + " is at least 5KB")
break
刚刚退出 while
循环,该循环会一直运行下去,因为它会测试 True
。这样可以避免对同一件事进行两次测试。
所以,你最终会得到:
import random
import os
def chosen():
return random.choice(os.listdir(r"/Users/me/p1/images/"))
def size(fn):
return os.path.getsize(r"/Users/me/p1/images/"+fn)
threshold = 5000
while True:
a_file = chosen()
file_size = size(a_file)
if file_size < threshold:
print(a_file + " is too small")
else:
break
print(a_file + " is at least 5KB")