我想使用while (从0到1000000的数字)运行一个函数(def) 100次
但while不会在需要时中断循环。我怎么才能修复它?
count = 2 #I start from 2 on purpose
limit = 101
def is_prime(i):
global count
count+=1
#there is more to the function is_prime but it isn't relevant
#i made a function with input to show a example
while (count < limit):
for i in range(1000000):
is_prime(i)
print ("count = ", count)我希望它在count =100时停止
发布于 2019-08-30 16:09:13
尝试这个方法--为了从子程序内部与全局变量交互--你需要指出它是全局的。否则,它只是与现有全局变量同名的局部函数变量
count = 2 #I start from 2 on purpose
limit = 101
def is_prime(i):
global count
count+=1
#there is more to the function is_prime but it isn't relevant
#i made a function with input to show a example
while (count < limit):
for i in range(1000000):
is_prime(i)
print ("count = ", count)
print(count)https://stackoverflow.com/questions/57722866
复制相似问题