一旦目标匹配,如何停止发电机?
How to stop a generator once target is matched?
正在尝试创建一个生成器,生成指定范围内的一组随机数字,然后在生成指定的目标数字后停止。将打印达到该数字的尝试次数。如果在指定的尝试次数内未生成该号码,则用户将收到单独的提示。这是我目前所拥有的:
try:
min_value = int(input("Enter the minimum value for your random generator: "))
max_value = int(input("Enter the maximum value for your random generator: "))
target = int(input("Enter the target value you are trying to find: "))
max_attempts = int(input("Enter the maximum number of attempts to find the target before stopping generation: "))
except ValueError:
print("Please enter an integer value for your input!")
def find_target(target: int, min_value: int, max_value: int, max_attempts: int) -> Optional[int]:
# Start counter for number of attempts
j = 0
while j in range(max_attempts):
#Increment the attempts counter
j += 1
for k in range(min_value, max_value):
if not target:
yield k
gen = find_target(target, min_value, max_value, max_attempts)
while True:
print(next(gen))
一旦找到目标,理想情况下会发生这样的事情:
# Stop the generator
print("Target acquired! It only took ", j, "tries to find the target!")
gen.close()
if find_target(target, min_value, max_value, max_attempts) is None:
print("Could not find target within the max number of attempts. Maybe better luck next time?")
现在生成器立即停止(我猜这与指定 if not target
的方式有关)。我怎样才能让这个逻辑起作用?
如果你想 return 定位你只需将 yield 放在 if 语句之后。
import random
def gen(low, high, attempts, target):
for j in range(1, attempts+1):
guess = random.randint(low, high)
yield guess
if guess == target:
print()
print("Target acquired! It only took ", j, "tries to find the target!")
break
else:
print()
print("Could not find target within the max number of attempts. Maybe better luck next time?")
low, high = 0, 10
attempts = 10
target = 5
for el in gen(low, high, attempts, target):
print(el, end=' ')
输出:
7 0 0 3 3 5
Target acquired! It only took 6 tries to find the target!
#or
4 2 0 6 1 4 3 6 1 6
Could not find target within the max number of attempts. Maybe better luck next time?
正在尝试创建一个生成器,生成指定范围内的一组随机数字,然后在生成指定的目标数字后停止。将打印达到该数字的尝试次数。如果在指定的尝试次数内未生成该号码,则用户将收到单独的提示。这是我目前所拥有的:
try:
min_value = int(input("Enter the minimum value for your random generator: "))
max_value = int(input("Enter the maximum value for your random generator: "))
target = int(input("Enter the target value you are trying to find: "))
max_attempts = int(input("Enter the maximum number of attempts to find the target before stopping generation: "))
except ValueError:
print("Please enter an integer value for your input!")
def find_target(target: int, min_value: int, max_value: int, max_attempts: int) -> Optional[int]:
# Start counter for number of attempts
j = 0
while j in range(max_attempts):
#Increment the attempts counter
j += 1
for k in range(min_value, max_value):
if not target:
yield k
gen = find_target(target, min_value, max_value, max_attempts)
while True:
print(next(gen))
一旦找到目标,理想情况下会发生这样的事情:
# Stop the generator
print("Target acquired! It only took ", j, "tries to find the target!")
gen.close()
if find_target(target, min_value, max_value, max_attempts) is None:
print("Could not find target within the max number of attempts. Maybe better luck next time?")
现在生成器立即停止(我猜这与指定 if not target
的方式有关)。我怎样才能让这个逻辑起作用?
如果你想 return 定位你只需将 yield 放在 if 语句之后。
import random
def gen(low, high, attempts, target):
for j in range(1, attempts+1):
guess = random.randint(low, high)
yield guess
if guess == target:
print()
print("Target acquired! It only took ", j, "tries to find the target!")
break
else:
print()
print("Could not find target within the max number of attempts. Maybe better luck next time?")
low, high = 0, 10
attempts = 10
target = 5
for el in gen(low, high, attempts, target):
print(el, end=' ')
输出:
7 0 0 3 3 5
Target acquired! It only took 6 tries to find the target!
#or
4 2 0 6 1 4 3 6 1 6
Could not find target within the max number of attempts. Maybe better luck next time?