Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/338.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何调用已经结束的循环? x=0 而x_Python_Python 3.x - Fatal编程技术网

Python 如何调用已经结束的循环? x=0 而x

Python 如何调用已经结束的循环? x=0 而x,python,python-3.x,Python,Python 3.x,通过这种方式,您可以永远循环它: x=0 while x<=10: x+=1 但我认为您需要的是这样一个简洁的函数: while True: x=0 while x<=10: x+=1 while True: x=0 while x<=10: x+=1 break def循环n次(n): 循环=0 当循环

通过这种方式,您可以永远循环它:

x=0
while x<=10:
    x+=1
但我认为您需要的是这样一个简洁的函数:

while True:
    x=0
    while x<=10:
        x+=1
while True:
    x=0
    while x<=10:
        x+=1
    break
def循环n次(n):
循环=0
当循环而x将代码放入函数中,并在需要时再次调用它

def loop_n_times(n):
    loop = 0
    while loop < n:
        x = 0
        while x <= 10:
            x += 1
        loop += 1

再绕一圈?你到底想做什么?有很多方法可以重用代码,最好的方法取决于用途。请解释您试图解决的实际问题。将代码放入函数中,并在需要时再次调用。是的,这很有效,谢谢!但接下来你需要展示如何在为True时打破该

def count():
    x=0
    while x<10:
        x+=1
        print(x)

count() # run loop once
count() # run loop again
1
2
3
4
5
6
7
8
9
10
1
2
3
4
5
6
7
8
9
10